Sending POST Requests
Posting Raw Data
We can send simple POST requests to upstream systems with the request
action as follows:
The supplied JSON response from https://httpbin.org/
reflects the request we've just sent:
Let's now send some data using the body
field. Since the HTTP method will be automatically set to POST
, we don't need to specify it explicitly:
The response shows that FLAT sends the data as text/plain
:
📎 By the way, have you noticed how we XML-encoded the ampersand as
&
in the request?
To send the data as URL-encoded form data answer=41+1&foo"
we also have to set the MIME type accordingly:
Good! But still not what we wanted! The +
got lost. Not only do we have to consider the XML context we're in (&
), but we also need to URL-encode the data ourselves, as the body
field contains the raw POST data as they will be sent upstream:
Or, with the help of the urlencode
function:
Yeah, that hurts pretty much!
The post
field to the rescue!
Posting Form Data
We can achieve the same result with the following, much simpler request using the post
field. The POST data is assembled automatically, URL-encoding included:
Thus, for submitting URL-encoded form data, the post
field is the way to go.
Posting JSON
Sending JSON data is easy. We just supply the desired JSON as object for value
:
The MIME type is set automatically:
To send data from a JSON file we might use the json-doc
function:
See below for a general way to send content from a file.
Posting XML
To send XML data we set the MIME type to text/xml
and supply the XML payload in the body
field:
📎 Instead of using
<![CDATA[…]]>
we could have written<answer>…
.
Reading POST Data from Files
Sending XML like this is again rather ugly. Fortunately, the body does not have to be placed inline. We can also make use of the body
field's src
property to specify a location from where the body content should be read:
This way is binary safe, so we're able to send arbitrary content, an image for example:
As before, the MIME type can be explicitly set with mime
or, if missing, FLAT tries to determine it automatically:
Last updated