FLAT
CouperSevenval TechnologiesDocker ImageGithub
master
master
  • Changelog
  • FLAT
  • Administration
    • Configuration
    • Docker
    • Logging
  • Cookbook
    • Using the Built-in Mocking
    • Performing Additional Checks on JWT Access Tokens
    • Logging Custom Fields
    • Using Environment Variables
    • Handling Errors with an Error Flow
    • File Serving
    • Forwarding a Request to an Upstream API
    • Extracting Common Initialization Flow Tasks
    • Encoding and Decoding JWT
    • Passing Header Fields to the Client
    • How can I pass an arbitrary header field to an upstream system?
    • Performing Additional Checks on JWT Access Tokens
    • Proxying requests to Upstream APIs
    • Increasing the Request Timeout
    • How can I see what the client requested?
    • Using Swagger UI for API Documentation
    • Testing API Requests
    • Testing with Backend Requests
    • Testing Templates
    • Sending POST Requests
    • Processing Upstream Responses
    • Protecting Access using JWT Tokens
  • Reference
    • Configuration
    • Debugging
    • flat CLI
    • Flow
    • Variables
    • OpenAPI / Swagger Integration
    • OpenAPI
      • CORS - Cross-Origin Resource Sharing
    • OpenAPI
      • Differences from Swagger
    • OpenAPI
      • Mocking
    • OpenAPI
      • Routing
    • OpenAPI
      • Security
    • OpenAPI
      • Upstream APIs
    • OpenAPI
      • Validation
    • Flow Actions
      • assert Action
      • auth Action
      • backend-flow Action
      • copy Action
      • debug Action
      • dump Action
      • echo Action
      • error Action
      • eval Action
      • log Action
      • nameshave Action
      • pass-body Action
      • proxy-request Action
      • regex Action
      • request Action
      • requests Action
      • serve Action
      • set-config Action
      • set-env Action
      • set-response-headers Action
      • set-status Action
      • sub-flow Action
      • template Action
      • test-request Action
      • xslt Action
    • Functions
      • apply-codecs()
      • array-reverse()
      • array()
      • base64-decode()
      • base64-encode()
      • body()
      • calc-signature()
      • capitalize-first()
      • content()
      • decrypt-xml()
      • decrypt()
      • encrypt()
      • ends-with()
      • file-exists()
      • fit-document()
      • fit-log()
      • fit-serialize()
      • get-log()
      • has-class()
      • html-parse()
      • join()
      • json-doc()
      • json-parse()
      • json-stringify()
      • json-to-csv()
      • json-to-xml()
      • jwt-decode()
      • jwt-encode()
      • ldap-lookup()
      • ldap-query()
      • lookup()
      • matches()
      • md5()
      • replace()
      • sort()
      • split()
      • tolower()
      • toupper()
      • trim()
      • unixtime()
      • urldecode(), url-decode()
      • urlencode(), url-encode()
      • uuid3() and uuid4()
      • verify-signature()
      • verify-xmldsig()
      • xml-parse()
      • xml-to-json()
    • Templating
      • {{,}}
      • Comment {{// …}}
      • Dot {{.}}
      • Conditional `{{if <condition>}} … {{elseif <condition> }} … {{else}} … {{end}}
      • loop
      • ?? Operator
      • Object XML Notation (OXN)
      • Pair Producer {{: …}}
      • Placeholder
      • Template Variables
      • with
    • Testing
  • Tutorial
Powered by GitBook
On this page
  • Posting Raw Data
  • Posting Form Data
  • Posting JSON
  • Posting XML
  • Reading POST Data from Files

Was this helpful?

  1. Cookbook

Sending POST Requests

PreviousTesting TemplatesNextProcessing Upstream Responses

Last updated 5 years ago

Was this helpful?

Posting Raw Data

We can send simple POST requests to upstream systems with the as follows:

<flow>
  <request>{ "url": "https://httpbin.org/post", "method": "POST" }</request>
</flow>

The supplied JSON response from https://httpbin.org/ reflects the request we've just sent:

{
  "args": {},
  "data": "",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "deflate, gzip",
    "Content-Length": "0",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "httpbin.org",
    "User-Agent": "FLAT"
  },
  "json": null,
  "origin": "88.130.59.205, 88.130.59.205",
  "url": "https://httpbin.org/post"
}
<flow>
  <request>
  {
    "url": "https://httpbin.org/post",
    "body": {
      "value": "answer=41+1&amp;foo"
    }
  }
  </request>
</flow>

The response shows that FLAT sends the data as text/plain:

{
  …
  "data": "answer=41+1&foo",
  …
  "form": {},
  "headers": {
    …
    "Content-Length": "15",
    "Content-Type": "text/plain",
    …
  },
  …
}

📎 By the way, have you noticed how we XML-encoded the ampersand as &amp; 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:

    …
    "body": {
      "mime": "application/x-www-form-urlencoded",
      "value": "answer=41+1&amp;foo"
    }
    …
{
  …
  "data": "",
  …
  "form": {
    "answer": "41 1",
    "foo": ""
  },
  "headers": {
    …
    "Content-Type": "application/x-www-form-urlencoded",
    …
  },
  …
}

Good! But still not what we wanted! The + got lost. Not only do we have to consider the XML context we're in (&amp;), 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:

      "value": "answer=41%2B1&amp;foo"
      "value": {{ concat("answer=", urlencode("41+1"), "&amp;foo") }}

Yeah, that hurts pretty much!

Posting Form Data

<flow>
  <request>
  {
    "url": "https://httpbin.org/post",
    "post": [
      {"name": "answer", "value": "41+1" },
      {"name": "foo",    "value": "" }
    ]
  }
  </request>
</flow>

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:

<flow>
  <request>
  {
    "url": "https://httpbin.org/post",
    "body": {
      "value": { "answer": 42 }
    }
  }
  </request>
</flow>

The MIME type is set automatically:

{
  …
  "data": "{\"answer\":42}",
  …
  "headers": {
    …
    "Content-Type": "application/json",
    …
  },
  "json": {
    "answer": 42
  },
  …
}
    …
    "body": {
      "value": {{ json-doc("/files/answer.json") }}
    }
    …

Posting XML

To send XML data we set the MIME type to text/xml and supply the XML payload in the body field:

<flow>
  <request>
  {
    "url": "https://httpbin.org/post",
    "body": {
      "mime": "text/xml",
      "value": "<![CDATA[<answer>42</answer>]]>"
    }
  </request>
</flow>

📎 Instead of using <![CDATA[…]]> we could have written &lt;answer>….

{
  …
  "data": "<answer>42</answer>",
  …
  "headers": {
    …
    "Content-Length": "19",
    "Content-Type": "text/xml",
    …
  },
  …
}

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:

    …
    "body": {
      "mime": "text/xml",
      "src": "fit://site/files/answer.xml"
    }
    …

This way is binary safe, so we're able to send arbitrary content, an image for example:

<flow>
  <request>
  {
    "url": "https://httpbin.org/post",
    "body": {
      "src": "fit://site/files/image.jpg"
    }
  }
  </request>
</flow>

As before, the MIME type can be explicitly set with mime or, if missing, FLAT tries to determine it automatically:

{
  …
  "data": "data:application/octet-stream;base64,/9j/4AAQSkZJR…
  …
  "headers": {
    …
    "Content-Length": "1689",
    "Content-Type": "image/jpeg",
    …
  },
  …
}

Let's now send some data using the . Since the HTTP method will be automatically set to POST, we don't need to specify it explicitly:

Or, with the help of the :

The to the rescue!

We can achieve the same result with the following, much simpler request using the . The POST data is assembled automatically, URL-encoding included:

To send data from a JSON file we might use the :

See for a general way to send content from a file.

request action
urlencode function
json-doc function
below
body field
post field
post field