[Multipart/form-data] Demo

Forge Component
(6)
Published on 2019-09-06 by João Barata
6 votes
Published on 2019-09-06 by João Barata

Kindly show some demo, how it work. 

i am using Postman to test the API, its has one json   and binary file in body content. 

Solution

Hi Mohan,

I don't have a demo (because that would need a REST service that needs a multipart/form-data, and it's not easily possible to create it in OutSystems). However, these are the steps to take:

  1. Create a List of Part Structures, each Part containing the desired content. In your case, the JSON goes in the Content Attribute of one item, and the binary file goes in the ContentBinary Attribute of the other. Make sure to set the right Name, as required by the API you're connecting to.
  2. Call MultipartFormDataCreate, use the Part List as input. You can omit the Boundary Parameter, it will be created for you (though if you require a specific boundary, you can specify it of course).
  3. Call your API. Typically, you'll need to specify the Content-Type, for which you can use the ContentTypeGet Function. As input it needs the boundary, which is the BoundaryUsed Output Parameter of MultipartFormDataCreate. See below on how to subsequently use the ContentType.
  4. As content for your API Method, you specify the MessageBinary Output Parameter of MultipartFormDataCreate.

In order for your REST API to work properly, you will need to set the right Content-Type in the header. To do so, specify an Input Parameter ContentType, with its "Send In" Property set to "HeadeR" and its "Name in Request" Property set to "Content-Type":

The binary content should be sent in the body ("Send In" set to "Body"), and its data type should of course be "Binary Data".

If you need an example, you can take a look at my ValidSign Component, which uses Multipart/form-data as described above.

Solution

Kilian Hekhuis wrote:

Hi Mohan,

I don't have a demo (because that would need a REST service that needs a multipart/form-data, and it's not easily possible to create it in OutSystems). However, these are the steps to take:

  1. Create a List of Part Structures, each Part containing the desired content. In your case, the JSON goes in the Content Attribute of one item, and the binary file goes in the ContentBinary Attribute of the other. Make sure to set the right Name, as required by the API you're connecting to.
  2. Call MultipartFormDataCreate, use the Part List as input. You can omit the Boundary Parameter, it will be created for you (though if you require a specific boundary, you can specify it of course).
  3. Call your API. Typically, you'll need to specify the Content-Type, for which you can use the ContentTypeGet Function. As input it needs the boundary, which is the BoundaryUsed Output Parameter of MultipartFormDataCreate. See below on how to subsequently use the ContentType.
  4. As content for your API Method, you specify the MessageBinary Output Parameter of MultipartFormDataCreate.

In order for your REST API to work properly, you will need to set the right Content-Type in the header. To do so, specify an Input Parameter ContentType, with its "Send In" Property set to "HeadeR" and its "Name in Request" Property set to "Content-Type":

The binary content should be sent in the body ("Send In" set to "Body"), and its data type should of course be "Binary Data".

If you need an example, you can take a look at my ValidSign Component, which uses Multipart/form-data as described above.

Hi Killian,


Good day. I am having trouble using the forge component MultipartFormData. The case is we are trying to recreate a working native API endpoint in Outsystems. We are going to expose an API from OS and there will be a flow where in we will consume that native API endpoint.

Here's the thing :

1. It requires a multipart form request. see image below

2. I have tried using parts add for these 5 inputs and then use the SA MultipartFormDataCreate. see image below



But I keep getting this response. See image below



Your advise/help will be highly appreciated. Thank you and have a nice day!


Cheers,

Eruel

Hi Eruel,

The "422" error code typically specifies that the data was received correctly - which would mean that the multi-part/formdata is correct - but that the content itself somehow wasn't correct.

Looking at the Postman input, it seems that target_number, source_number and check_number aren't files, and therefore I don't think you should add them as multi-part/formdata. I'm not entirely sure how you should add them though, whether they should all go in a single part or not. It would be useful to have the exact Postman message, does Postman show the raw input somewhere?

Kilian Hekhuis wrote:

Hi Eruel,

The "422" error code typically specifies that the data was received correctly - which would mean that the multi-part/formdata is correct - but that the content itself somehow wasn't correct.

Looking at the Postman input, it seems that target_number, source_number and check_number aren't files, and therefore I don't think you should add them as multi-part/formdata. I'm not entirely sure how you should add them though, whether they should all go in a single part or not. It would be useful to have the exact Postman message, does Postman show the raw input somewhere?

Hi Kilian,

No available raw input on postman. See image below

inputs are only indicated on form-data.

Here's the example of the successful response message.



Cheers,

Eruel

Hi Kilian,

Can I just put the target_number, source_number and check_number as URL parameters? What do you think?

Thanks!

Eruel

Hi Eruel,

I don't think that works, as it should be form-data. However, it seems I was wrong, at least partially. I created a Postman request with two text parameters and one file parameter, and sent the request to a REST service in OutSystems, increasing the logging level so I could capture what was sent.

Postman parameters:

Logging in OutSystems:

So it seems that the "name" part should indeed be "target_number" etc.

Hi Kilian,


So it is correct on how I used the PartsAdd functionality? But why does it return a 422 response?

Well, like I said, 422 typically means "Data received ok, but content is not ok". So it might just be you send it stuff it doesn't expect, even though the multi-part/formdata is correct. To troubleshoot this, I'd advise you the following:

  • Log the MessageBinary output of the MultipartFormDataCreate Action, by converting it to Text (use the BinaryDataToText Action from the BinaryDataExtension) and then logging that Text (use LogMessage from System). In the Service Center logging you should be able to see it in the General Log. Check whether it looks similar to what I showed above.
  • As a last resort: capture the Postman output. There's of course several ways to do it, but perhaps the quickest way is to expose a dummy REST Service with a single Method, with a single Input Parameter of type Text. It needn't have a body, it's just for logging. In Service Center, increase the logging level of the new REST Service to Full. In Postman, change the URL of the external service to the URL of the new REST Method (you can copy/paste it from the REST Service's Documentation page). Send the request. You'll get an error ("The request entity's media type 'multipart/form-data' is not supported for this resource.") but that's ok. Check the Integration Logging. The Postman request should be logged there (it's what I did to capture Postman's output above).

Hi Kilian,

Im getting this message on general logs.

Why does it only shows the front_image?

If the log is too long, it cuts it off. You should click the "Download HTTP Trace" to see if that's indeed everything.

If it is, then you might not chain the PartAdds right. You should always use as "Parts" input the "PartsAdded" output of the previous. Debug the code, and verify that the "Parts" input of MultipartFormDataCreate indeed contains all the parts.