HTTP tutorial - Using phase 3

In phase 3, you have numerous options for accessing external resources via HTTP.

Function "http"


With this function, you can configure and send an HTTP request similar to phase 6. In contrast to phase 6, the response is immediately available to you because it is stored in a map. Thus, you can solve the task from example 5 in phase 3. To generate the JSON structure, use the function create json from node.

Function "http json-lookup"


With this function, a JSON file can be read in as an entire object or individual objects extracted from it via XPath expressions using an HTTP request.

Function "call SOAP-WebService"


This function is available in two versions for web services that are to be called from the mapping without and with a template file.

In the following, an HTTP call is created in phase 3 and JSON is used as the document type. Create a new profile:


  • Document type: CSV

  • Mapping type: Data Routing

  • Phase 1 - Event-based Input Agent Manual upload.

  • Phase 3 - Create a top level node in the target structure and below it a field createJSON with the function concat(a,b,c,d,e,f,g,h, [CR-support i]) to assemble the content of a simple JSON:


Function - concat
createJSON
 
concat(a,b,c,d,e,f,g,h, [CR-support i])
a value: { "OP1":
b   value: 42
c   value: , "OP2":
d   value: 24
e   value: , "OPR": "
f   value: +
g   value: " }
h   value:
i   value:


The calculation values 42 and 24 as well as the operator can be specified for testing or alternatively taken dynamically from a database or variable.

For complex structures, which are quite common with JSON and XML, we recommend building them in the target structure. The generation of the file is assigned to the functions create json from node for JSON or create xml from node for XML.

Once the structure to be transmitted has been created, it must be sent to the target system. For this purpose, the http function is used, which you can use on a calculation field.


Function - http
calc_callhttp
 
http(a,b,c,d,e,f,g,h,i,j,k,l,m,n)
a value: https://<TARGET-URL>
b value:
c value:
d target field: createJson
e value:
f value: application/json
g value: POST
h value:
i value:
j value:
k value:
l value: map_result_http
    m value:
n value:


  • In parameter a, enter the target address, e.g. https://<Server>/dw/Request/calculator_json_body

  • The JSON as transmitted content for parameter d was created here in the target field createJSON.

  • The parameter f requires the MIME type.

  • For parameter g, define the method used, here POST.

  • Parameter l requires the name of a map, in which the response is stored along with important metadata, such as the status.


images/download/attachments/137306105/1588-version-1-modificationdate-1686900075304-api-v2.png


Perform a mapping test by uploading any test file and the service should be called.

In order to now use the result in phase 3 in case of success, JSON structure { "RESULT": "66" }, it must be fetched via the map key RESPONSE_HTTP_DATA. The following function chain shows an example of how the desired result is read from the response and stored in a variable var__result, for example.


getResult
getResult
 
get value from map(key a, name of map b, delimiter c, item d, [default e])
a value: RESPONSE_HTTP_DATA
b value: map_result_http
c value:
d value:
e value:
 
parse json and add to cache(data a, key b)
a result: 1
b value: json-data
 
execute json xpath(key a, expression b, array as text c, ignore exception d)
a value: json-data
b value: $[*].RESULT
c value: true
d value:
 
save variable a(b) type-safe
a value: var__result
b result: 3
 
remove parsed json from cache(key a)
a value: json-data


Here, the result from the map get value from map was first transferred into an internal structure with the function parse json and add to cache. Using the subsequent function execute json xpath, an object can be queried specifically at field level via XPath. In order for the internal structure not to remain in the memory and waste resources, it is removed again with function remove parsed json from cache.


images/download/attachments/137306105/1589-version-1-modificationdate-1686900075321-api-v2.png


This example can also be constructed with a single function http json-lookup as described above.