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 | Handling HTML form encoding using a predefined generic event definition
 
Handling HTML form encoding using a predefined generic event definition
You can invoke an HTTP service with a payload encoded to either multipart/form-data or application/x-www-form-urlencoded media types using the predefined FormRequest event definition. For detailed information about this event definition, see the API Reference for EPL (ApamaDoc) .
The FormRequest event definition must be used if metadata.contentType is set to either multipart/form-data or application/x-www-form-urlencoded. The request payload must be a dictionary having a string key and string value.
If metadata.contentType is set to application/x-www-form-urlencoded, then the dictionary payload is transmitted as URL-encoded form data.
If metadata.contentType is set to multipart/form-data, then the dictionary payload is encoded to multi-part form data.
Note:
Binary data cannot be read in Apama EPL. Hence it is only possible to send non-ASCII text data form fields with a standard HTTPClientGenericJSONChain.
Simple example
Use multipart/form-data and application/x-www-form-urlencoded media types with non-ASCII text data form fields:
monitor TestHtmlEncoding
{
action onload()
{
dictionary<string, string> payload := {"foo":"bar", "abc":"def"};

dictionary<string, dictionary<string, string>> formMetadata :=
new dictionary<string, dictionary<string, string>>;

HttpTransport transport :=
HttpTransport.getOrCreateWithConfigurations("my_host",
8080, new dictionary<string, string>);
HttpOptions httpOptions := new HttpOptions;

// Using application/x-www-form-urlencoded media type
httpOptions.headers["content-type"] := "application/x-www-form-urlencoded";
FormRequest(
transport.createRequest(RequestType.POST, "/", payload, httpOptions),
formMetadata).execute(handleResponse);

// Using multipart/form-data media type
httpOptions.headers["content-type"] := "multipart/form-data";
FormRequest(
transport.createRequest(RequestType.POST, "/", payload, httpOptions),
formMetadata).execute(handleResponse);
}

action handleResponse(Response resp)
{
log "Got response: " + resp.toString() at INFO;
}
}
For multipart/form-data, you can still encode binary data form fields. But to do that, you need to develop a custom plug-in which introduces binary data in your customized chain. In that case, the binary data form fields must have the following additional metadata:
*filename
*contentType
*charset
filename is a required parameter. You can provide this metadata as follows:
monitor TestHtmlEncodingBinaryFields
{
action onload()
{
dictionary<string, string> payload :=
{"foo":"bar", "binary_field": ...binary_data};

dictionary<string, dictionary<string, string>> formMetadata := {
"binary_field":{
"filename":"file1.txt",
"charset":"utf-8",
"contentType":"text/plain"
}
};

HttpTransport transport :=
HttpTransport.getOrCreateWithConfigurations("my_host",
8080, new dictionary<string, string>);
HttpOptions httpOptions := new HttpOptions;

// Using multipart/form-data media type
httpOptions.headers["content-type"] := "multipart/form-data";
FormRequest(transport.createRequest(RequestType.POST, "/", payload,
httpOptions), formMetadata).execute(handleResponse);
}

action handleResponse(Response resp)
{ log "Got response: " + resp.toString() at INFO; }
}