Altova MapForce 2023 Enterprise Edition

Handling the HTTP Response Conditionally

Home Prev Top Next

Let's assume that a Web service returns either JSON or XML content type in the response body, depending on the value of the Accept HTTP request header. Additionally, in case of an error (status code > 299), the Web service may return an undefined content type. This is a case where it might be useful to handle the response conditionally based on the HTTP status code and content type.


To handle both the JSON and the XML response from the Web service in the same mapping, you could create multiple body items, by right-clicking the body item on the component and selecting Add Body Node After from the context menu.


On the mapping, a Web service component with multiple body items configured for the scenario above could look as follows:


The mapping illustrated above takes two input parameters. The first parameter provides the expected content type and its value can be application/json, or application/xml. The second parameter provides the numeric ID of the product to be retrieved.


If you provide the value application/json as input parameter, click the Preview ic-preview-buttondown button of the JSON target component. Otherwise, if the input parameter value is application/xml, click the Preview ic-preview-buttondown button of the XML target component. The respective output file (JSON or XML) will be generated if the Web service call is successful.


If the Web service call returns a status code greater than 300 (error), the HTTP response reaches the third body item. Since we don't know the exact content type, we choose to decode it with the help of the decode-mime-entity function. The Web service does not supply the character set value in the response header, which is why the constant value utf-8 is connected to the decoding function.


Each of the three response body items on the component above are configurable by clicking the excel1-compicon button next to each body:


1.The first body handles content returned as application/json if the status code is between 200 and 299. Also, the schema of the expected JSON response structure is set.


2.The second body handles content returned as application/xml if the status code is between 200 and 299. Also, the schema of the expected XML response structure is set.


3.The third body handles content returned as */* if the Web service returns an erroneous status code (between 300 and 599).


© 2016-2022 Altova GmbH