How to consume API with input as file

I have a post API with two parameters, one of them is a file. How can I pass this file to the Consume API action. I have researched a lot but everything seems very vague. Is there any way to solve this problem? Please give me a specific example. Thank you.

Hi Khai Kieu Minh,

It totally depends on the type of API. Does it use multipart/form-data? Or does it expect a base64-encoded binary in a JSON attribute?

Kilian Hekhuis wrote an article about how to consume multipart/form-data REST services.

https://itnext.io/consuming-a-multipart-form-data-rest-method-with-outsystems-c466e404118d

Note that in the article Kilian Hekhuis mentioned above there should be enough pointers for you to get started, see especially the "Implementation details":

  • declare the REST API Method having a Parameter with Data Type “Binary Data”, and its “Send In” Property set to “Body”;
  • you then need to add [a Content-Type tag] (via a Parameter with “Send In” set to “Header”) [see below];
  • you need the Binary Concat Forge Component, as well as Actions from the BinaryData Extension (which is a System Component, so already installed).

Then regardless of the specific solution that makes sense for your project, Kilian Hekhuis created a Forge Component, aptly name Multipart/form-data, that consists of a single eSpace with a number of Public Actions that aid in creating the right content:

  • MultipartFormDataCreate?—?the main Action, creates a text body consisting of the specified parts, in a multipart/form-data compatible format.
  • ContentTypeGet?—?returns the value to be used for the Content-Type header of the REST message based on the boundary used.
  • PartAdd?—?a helper Function that can be used to easily create a List of parts."

So what you need to to do is create a list of parts (using PartAdd or just ListAppend), feed it to MultipartFormDataCreate, use its output parameter BoundaryUsed as input to ContentTypeGet, and use MultipartformatDataCreate.MessageBinary as input to the Binary Parameter of your consumed REST Service, and ContentTypeGet.ContentType as input to the content type Input Parameter (or use it as a Function directly).

Hope this helps!
Thanks

Viraj Kataria wrote:

Hi Khai Kieu Minh,

It totally depends on the type of API. Does it use multipart/form-data? Or does it expect a base64-encoded binary in a JSON attribute?

Kilian Hekhuis wrote an article about how to consume multipart/form-data REST services.

https://itnext.io/consuming-a-multipart-form-data-rest-method-with-outsystems-c466e404118d

Note that in the article Kilian Hekhuis mentioned above there should be enough pointers for you to get started, see especially the "Implementation details":

  • declare the REST API Method having a Parameter with Data Type “Binary Data”, and its “Send In” Property set to “Body”;
  • you then need to add [a Content-Type tag] (via a Parameter with “Send In” set to “Header”) [see below];
  • you need the Binary Concat Forge Component, as well as Actions from the BinaryData Extension (which is a System Component, so already installed).

Then regardless of the specific solution that makes sense for your project, Kilian Hekhuis created a Forge Component, aptly name Multipart/form-data, that consists of a single eSpace with a number of Public Actions that aid in creating the right content:

  • MultipartFormDataCreate?—?the main Action, creates a text body consisting of the specified parts, in a multipart/form-data compatible format.
  • ContentTypeGet?—?returns the value to be used for the Content-Type header of the REST message based on the boundary used.
  • PartAdd?—?a helper Function that can be used to easily create a List of parts."

So what you need to to do is create a list of parts (using PartAdd or just ListAppend), feed it to MultipartFormDataCreate, use its output parameter BoundaryUsed as input to ContentTypeGet, and use MultipartformatDataCreate.MessageBinary as input to the Binary Parameter of your consumed REST Service, and ContentTypeGet.ContentType as input to the content type Input Parameter (or use it as a Function directly).

Hope this helps!
Thanks

Hi Viraj,

Thank you for your help. I have read that article many times, but there are some issues that are not clear, can you help me?
1. When I use PartAdd action, what value must I enter into the Parts property?

2. My API needs to have 2 parameters, so I have to call PartAdd 2 times, right?

Please help me. Thank you very much.

Regards,

Khai