Handling HTML form encoding
If the body of the request is a dictionary payload having a string key and either a string or binary value, the request body is then encoded to either multipart/form-data or application/x-www-form-urlencoded media types, depending on metadata.contentType.
If metadata.contentType is set to application/x-www-form-urlencoded, then the dictionary payload must have string keys and string values and 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. This method must be used to send non-ASCII text or binary data. The binary data form fields should have the following additional metadata: filename, contentType and charset. filename is a required parameter.
You can put these metadata items in a form dictionary as follows:
metadata.http.form.name.contentType
metadata.http.form.name.charset
metadata.http.form.name.filename
where name corresponds to the data in payload.name.
Simple example
Send a dictionary payload request body which has both key and value strings using the application/x-www-form-urlencoded method:
event HTTPRequestURLEncoding {
integer id;
string method;
string path;
string contentType;
dictionary<string, string> data;
}
Send a dictionary payload request body which has a string key and either a string or binary value using the multipart/form-data method; provide the metadata for binary form data using formMetadata:
event HTTPRequestMultiPartForm {
integer id;
string method;
string path;
string contentType;
dictionary<string, string> data;
dictionary<string, dictionary<string,string>> formMetadata;
}
Send a request:
monitor TestFormEncoding {
action onload() {
dictionary<string, string> dataURL :=
{"string":"Hello World", "foo":"bar"};
dictionary<string, string> dataMultiPart :=
{"string":"Hello World", "binary": Binary Data};
//Metadata for form data filed
dictionary<string,dictionary<string,string>> formMetadata := {
"binary":{
"filename":"file.txt",
"charset":"utf-8",
"contentType":"text/plain"
}
};
integer id := integer.incrementCounter("HTTPClient.requestId");
//Using application/x-www-form-urlencoded media type
send HTTPRequestURLEncoding(id, "POST", "/",
"application/x-www-form-urlencoded", dataURL) to "http";
id := integer.incrementCounter("HTTPClient.requestId");
//Using multipart/form-data media type
send HTTPRequestMultiPartForm(id, "POST", "/", "multipart/form-data",
dataMultiPart, formMetadata) to "http";
}
}
Map the metadata of binary form data using the Mapper codec:
- mapperCodec:
HTTPRequestMultiPartForm:
towardsTransport:
mapFrom:
- metadata.requestId: payload.id
- metadata.http.method: payload.method
- metadata.http.path: payload.path
- metadata.contentType: payload.contentType
- metadata.http.form.binary.contentType:
payload.formMetadata.binary.contentType
- metadata.http.form.binary.filename:
payload.formMetadata.binary.filename
- metadata.http.form.binary.charset:
payload.formMetadata.binary.charset
- payload: payload.data