Calculator via POST method with JSON input and output

In this example, you will build a calculator web service that handles the 4 basic arithmetic operations, supporting only integers. The data, i.e. two operands and the operator, are passed within a JSON in the body of a POST request.

Please create a simple JSON file for this purpose. Here is an example based on an addition operation:


Input
{
"OP1": 42,
"OP2": 24,
"OPR": "+"
}
Output
{
"RESULT": 66
}


The objects OP1 and OP2 receive the integer values. The arithmetic operations (+), (-), (x), (/) are transferred in object OPR.

The response should also be returned in the body with HTTP status 200 in case of success and HTTP status 500 in case of error.

Step 1 - Create profile


You are welcome to clone the previous profile, as you can adopt a few configurations, e.g. the dynamic response in phases 1 and 6.

Please activate the mapping in the Main settings and select the document type JSON with character encoding UTF-8. Adjust the name of the profile, e.g. Lobster_Tutorial_HTTP_calculator_json_body.


images/download/attachments/137305186/basis_data-version-1-modificationdate-1686289576348-api-v2.png

Step 2 - Adjust input settings


In phase 1, use the event-based Input Agent of type HTTP with the following settings:


  • URL address suffix: calculator_json_body

  • Method: POST (do not accept empty body).

  • The response is again sent with a custom class.

  • The HTTP error code is 500.

  • There is no access control and no parameters.

  • Phase 2 is not edited.


images/download/attachments/137305186/phase_1-version-2-modificationdate-1686649899491-api-v2.png


In this profile, the address of the HTTP POST request is http://<Server>/dw/Request /calculator_json_body , followed by the body of the request.

Step 3 - Mapping


Create a top level node in the source structure with any name, e.g. input, the node does not need a matchcode.

Below it, add three fields at the same level: OP1, OP2 and OPR. The names are formed from the JSON elements. The fields need a matchcode that checks for equality of field names and objects within the JSON.

The target structure has only one node, e.g. Output, with a field below it, e.g. RESULT.


images/download/attachments/137305186/phase3_2_EN-version-1-modificationdate-1686726961164-api-v2.png


This field contains the entire logic, which is created using the following functions:


RESULT:

1. save variable type-safe

a variable: var__result // the variable must be of type "integer"
b source field: OP1

2. calculate and save variable

a variable: var__result
b source field: OPR
c source field: OP2
d (remains empty)
e (remains empty)


Using the function calculate and save variable has the advantage that you can use the 'value', i.e. the character for the arithmetic operation, directly.

Step 4 - Configuration of Integration Unit


Note: The JsonCreationUnit has been set to deprecated since version 4.6. Please use the ExtendedJsonCreationUnit only!


For the output you need the JsonCreationUnit:


images/download/attachments/137305186/phase5_EN-version-1-modificationdate-1686726961167-api-v2.png


In order to obtain the desired format, you must deactivate the flags Force array creation... and Write top level nodes as objects.

Step 5 - Response in phase 6


As Response, use the custom class PassBackDataResponse. The content of the output file is built using the JsonCreationUnit.


images/download/attachments/137305186/phase6_EN-version-1-modificationdate-1686726961168-api-v2.png

Step 6 - Triggering the profile


To start the profile, you can use the Postman tool. To do this, create a new request:


  • Request name - give the request a meaningful name; to do this, you must go to "Save" in the request.

  • New Collection - create a new "collection" in which you can store the request.

  • Select the method POST

  • Enter a URL: http://<Server>/dw/Request /calculator_json_body

  • Add the body as raw and copy the JSON.

  • SEND.

  • You will receive the result in the body of the response.


In the log overview of the Control Center you will find the job of the executed profile.