Multipart form-data

Last modified by Erik Bakker on 2024/06/17 11:39

Sometimes when you call an external REST endpoint, they require you to send meta information and one or more "attachments" in one call to the REST endpoint. To make this possible, you need to send the information with the contentType called multipart/form-data. In this microlearning, we will discuss how you can configure a valid message within the eMagiz platform that allows you to send out messages with this contentType and have them accepted by the endpoint in question.

Should you have any questions, please get in touch with academy@emagiz.com.

1. Prerequisites

  • Expert knowledge of the eMagiz platform

2. Key concepts

This microlearning focuses on configuring a multipart/form-data message within eMagiz to ensure it is sent correctly to an endpoint.

Key aspects are:

  • Defining the boundary that separates the parts of the message
  • Defining the content types of each part of the message
  • Construction of the complete message according to the specification

3. Multipart form-data

Sometimes when you call an external REST endpoint, they require you to send meta information and one or more "attachments" in one call to the REST endpoint. To make this possible, you need to send the information with the contentType called multipart/form-data. In this microlearning, we will discuss how you can configure a valid message within the eMagiz platform that allows you to send out messages with this contentType and have them accepted by the endpoint in question.

Key aspects are:

  • Defining the boundary that separates the parts of the message
  • Defining the content types of each part of the message
  • Construction of the complete message according to the specification

The following criteria apply when utilizing the boundary functionality within the multipart/form-data construction:

  • The value of the Boundary must begin with a double horizontal bar –, this is called a leading hyphen
  • The value of the Boundary must not contain more than 70 characters in addition to the leading hyphen.
  • The value of the Boundary must not contain characters that are disabled by the HTTP protocol or the URL, such as the colon: etc.
  • A boundary within the request body must always be preceeded by a CRLF line. This means that when the request body ends with a CRLF line an additional CRLF line is needed before the boundary doubling the CRLF lines in that part of the request body.

3.1 Prepare the message

To construct the message, several steps are needed to make it work. Luckily, most of the steps necessary have to do with concepts we have already discussed in previous microlearnings. As you can imagine, based on what multipart/form-data entails, we need a way to both store the meta information and the file(s) we want to send to the external party. For example, put the metadata in one (or multiple) header(s) and use the file content as a payload. This you can achieve with a header enricher and standard transformer.

Once the file content is your payload, you must ensure that the data is "raw." So when you have a base64 encoded string, you should decode it before sending it to the endpoint.

On top of that, we need to define the contentType header.

expert-data-handling-multipart-form-data--content-type-header-config.png

3.2 Construct the message

After you have set the stage, you can use a standard transformer component to build your message correctly. To create it correctly, you need to take the following into account:

  • Each part of the message needs to be separated by a boundary
  • The message needs to start with a boundary and finish with a boundary
  • Line breaks are needed to differentiate between the boundary and the text content
  • No line break is needed when the content is not text-based

Given all this, you can write the following SpEL expression that will yield a desirable output:

'${multi-part-form-data.data-handling.boundary}' + headers.metaInfo + T(com.emagiz.util.Newline).CRLF.characters + '${multi-part-form-data.data-handling.boundary}' + T(com.emagiz.util.Newline).CRLF.characters + 'Content-Disposition: form-data; name="file"; filename="' + headers.filename + '"' + T(com.emagiz.util.Newline).CRLF.characters + 'Content-Type: application/pdf' + T(com.emagiz.util.Newline).CRLF.characters + T(com.emagiz.util.Newline).CRLF.characters + payload + '${multi-part-form-data.data-handling.boundary}'

Putting this in a standard transformation gives you the following solution in the flow.

expert-data-handling-multipart-form-data--standard-transformer-config.png

3.3 Calling the endpoint

Now that we have constructed our message correctly, the last thing to do is call the endpoint in question. Since we have prepared our message and accurately defined our contentType calling the endpoint does not require any additional configurations compared to what you are already used to when dealing with REST endpoints.

4. Key takeaways

  • Make sure to define the boundary that separates the parts of the message
  • Make sure to define the content types of each part of the message
  • Make sure to define the content type that matches the specification for multipart/form-data
  • Construct the complete message according to the specification

5. Suggested Additional Readings

If you are interested in this topic, please read the help texts on the platform and read the following link: