Altova MapForce 2025 Enterprise Edition

Conditional HTTP Responses

Home Prev Top Next

This topic explains how to handle HTTP responses conditionally. The scenario described in this topic assumes that a Web service returns a 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.

 

The scenario described in this topic is useful when you need to handle the response conditionally, based on the HTTP status code and content type.

 

Implementation

To handle JSON and XML responses from the Web service in the same mapping, you can create multiple Body items as follows: Right-click the Body item in the Web service component and select Add Body Node After from the context menu.

 

The screenshot below illustrates a mapping in which the Web service component accepts two input parameters and has three response Body items. The mapping also contains three output components.

mf_ws_conditional_body_04

Input parameters

The mapping illustrated above takes two input parameters. The first parameter provides a content type, which 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 an input parameter, click the ic-preview-buttondown button of the target JSON component. Otherwise, if the input parameter value is application/xml, click the ic-preview-buttondown button of the target XML component. Depending on the target component selected, a JSON or an XML file, respectively, 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 do not know the exact content type, we 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.

 

Response structures

Each of the three response Body items are configurable via their respective excel1-compicon buttons. When you click this button, the Response Structure dialog opens (screenshots below).

 

application/json

The first body handles content returned as application/json if the status code is between 200 and 299. The screenshot below shows how the JSON response structure is configured.

mf_ws_conditional_body_01

 

application/xml

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.

mf_ws_conditional_body_02

 

error

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

mf_ws_conditional_body_03

 

Important

It is important to note that a Web service call is made for each configured HTTP response. To avoid unnecessary Web service calls, it is recommended to create an intermediate file (not a variable), map all the outputs (Body items) to this intermediate file, and then split the intermediate file into the relevant number of outputs. The following tutorials will give you an idea of how to merge multiple sources into one target and how to split a source (or sources) into multiple targets:

 

Tutorial: Multiple Sources to One Target

Tutorial: Multiple Sources to Multiple Targets

Video Demo: One-to-Many Mapping

 

Also bear in mind that if the parent node of an output structure is not connected with the Response node of a Web service component, a Web service will be made for each child node separately. To prevent multiple Web service calls, you will need to provide the parent context, by connecting the parent node with the Response node. For details, see Parent Context in Web Services.

 

© 2018-2024 Altova GmbH