Apama 10.7.2 | Connecting Apama Applications to External Components | Standard Connectivity Plug-ins | The HTTP Client Transport Connectivity Plug-in | Mapping events between EPL and HTTP client requests | Mapping the body
 
Mapping the body
The HTTP client accepts and returns the payload as a binary object. What the payload consists of depends on the service to which you are connecting. Many services use string-based protocols (such as JSON). For these types of payload, you can use the String codec (see The String codec connectivity plug-in). On messages towards the transport, the String codec takes a string and encodes it in UTF-8 bytes. For messages towards the host, the String codec takes a byte array and decodes it to a string using the UTF-8 encoding. If you are using the String codec, you should put it as the last codec before the HTTP client.
To create a request with no payload (such as a GET request), you should pass an empty string to the String codec, which it will convert to a zero-byte payload. If you are using the generic JSON option (see also Using predefined generic event definitions to invoke HTTP services with JSON and string payloads), then you can do the same by sending a new any as the payload. For example:
transport.createRequest(RequestType.GET, "/path", new any, new HttpOptions);
The createGETRequest action will do this for you. In order to recreate this with your own custom chain using the JSON codec, then you need to have an empty payload (which will skip the JSON codec) and then use a second Mapper codec to add an empty string to the payload before the String codec:
- jsonCodec
- mapperCodec:
"*":
towardsTransport:
defaultValue:
payload: ""
- stringCodec
The resulting string can then be mapped directly into a field in an EPL event, or it can be further processed by other codecs (such as the JSON codec) before the resulting fields are mapped into the Apama event.
If you need to vary your processing depending on the type of the returned data, you may need to write a custom codec in order to handle this. To help with distinguishing different payload types, the HTTP client sets top-level fields to indicate the type of the payload. metadata.contentType contains the MIME type indicated in the Content-Type HTTP header. If present, then metadata.charset indicates the character set from the same HTTP header.