[Multipart/form-data] need to pass binary file to multipart/ form-data REST

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

I need to pass a binary file with certain parameters to a multipart/form-data REST service.
I am using Multipart/form-data component for the same. I am confused for the flow, I went through the demo too.
Can someone help me with steps to send the below data in body.

"account":"10101",
"contract":"101010",
"contractType":"test"
"userName":"abc@xyz.com"
*** binary file attached


Thanks,
Anagha

Hi Anagha,

In what format are the other parameters supposed to be delivered? JSON? Formdata?

Kilian Hekhuis wrote:

Hi Anagha,

In what format are the other parameters supposed to be delivered? JSON? Formdata?

Thank you for replying.
Its Form-data Kilian.
I added all those parameters as parts and then used multipartFormDatacreate action just like in the demo.
Then passed the content (multipartFormDatacreate.MessageBinary) as Binary data in body and Content-type(from multipartFormDatacreate.ContentType ) in header like the demo too.

I am now getting error that boundary not found. I am not providing the boundary anywhere. I suppose its taken by default by the component if not provided.
I am not sure how to proceed.

Thanks,
Anagha 


 

 

Hi Anagha,

Where are you getting that error? That is, which Action gives that error? Or is the error returned from the REST service you are calling?

Hi Kilian,

In service center I am seeing this getting from ActionRequestBoundaryGet which i have used earlier but now have removed from the code. I am not getting this error now, it was probably some caching issue.
Now I am getting Unable to parse incoming data response from the service code 400.

I think the easiest way to troubleshoot this is if you can supply the module so I can take a look at it.

Hello Kilian,
Please find the attached sample oml. Can you please take a look and help me. Its giving 400 after the service
Need to pass the file as binary as said above.

Thanks,
Anagha

Hi Anagha,

At a first glance everything looks ok. However, I cannot know if the actual format is correct for the REST API you are calling. For example, the REST API might need a single part with all parameters, URL-encoded or JSON, instead of each being a different part. But that's something I cannot know.

Error 400 is "bad request", which means the REST API doesn't understand what you're sending. If you increase the log level of the Integration, you should be able to get more details, the REST API is likely returning some more information than just the 400 error.

Anagha Chikate wrote:

Hello Kilian,

I got the postman request from client and this is the code that returns success in postman.

Cache-Control: no-cache
Postman-Token: 47a6fc7d-a60c-b082-fcce-cf7bcdae767a
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
	

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="userName"
	
email@abc.com
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="account"

1000
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="contract"

101010
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="contractType"

Priced Contract
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name=""; filename="SignedDoc.pdf"
Content-Type: application/pdf
	
------WebKitFormBoundary7MA4YWxkTrZu0gW--

But with the OS OML that I sent you I am not getting this output. Its working in postman.
We have to send all the other params as the form parts along with binary file.
Content type: multipart/form-data

What do I need to change in OS code? Maybe correct format is not being passed to the service?

That's exactly what I would expect the output of your module to be. To see what the final message looks like, you could add a BinaryDataToText with MultipartFormDataCreate.MessageBinary as input and inspect that, it should be the same content as the Postman output (except for the boundary).

 

Hello Kilian,

I am getting the below output from the module from the action MultipartFormDataCreate.MessageBinary by converting binary to base 64 and then base 64 to text .
But service is still returning bad request.

The file that is passed in postman request is a physical pdf file and the file that i am passing in the module is binary file. Will that make difference?


--------254eb9af-07af-470a-8430-6aca3e054f95

Content-Disposition: form-data; name="account"


79228

--------254eb9af-07af-470a-8430-6aca3e054f95

Content-Disposition: form-data; name="contract"


504363

--------254eb9af-07af-470a-8430-6aca3e054f95

Content-Disposition: form-data; name="ContractType"


Priced Contract

--------254eb9af-07af-470a-8430-6aca3e054f95

Content-Disposition: form-data; name="userName"


email@gmail.com

--------254eb9af-07af-470a-8430-6aca3e054f95

Content-Disposition: form-data; name="samplefile"; filename="simple.pdf"

Content-Type: application/pdf


<PDF>

--------254eb9af-07af-470a-8430-6aca3e054f95--



Hi Anagha,

I still see "ContractType" with a capital "C", while Postman has it with a lowercase "c" like all other fields. Are you sure this isn't the problem? Please retry with "contractType" instead.

Hi Kilian,

No thats not the problem.

Ok, thanks for checking. Another question: did you ever increase the log level, to check what the output of the REST call exactly is? I would expect the "message" to detail what it finds wrong.

Kilian Hekhuis wrote:

Ok, thanks for checking. Another question: did you ever increase the log level, to check what the output of the REST call exactly is? I would expect the "message" to detail what it finds wrong.


Yes Kilian.
This is the response its giving, nothing much in detail.

{"payload":{"exceptions":[{"userMessage":"Unable to parse incoming data."}],"status":{"code":"400","message":"Error"}}}

Attaching the stack trace too.
P.S : I have edited the service URL and Host name too.

Hi Anagha,

Where did you get the trace from? Is that the Integration trace from Service Center? Because it contains the request as Base64 instead of binary data (for example, a trace from a REST service we're using that also uses multipart/form-data looks like this:

POST https://my.host.nl/api/packages HTTP/1.1
User-Agent: OutSystemsPlatform
Content-Type: multipart/form-data; boundary="------9c2c9938-9667-4abe-9c98-ea7cee7359ea"
Accept: application/json; esl-api-version=11.18
Authorization: XXXXXXXX
Host: my.host.nl
Content-Length: 118528

<BINARY DATA>

HTTP/1.1 200 OK
Vary: Accept-Encoding
Transfer-Encoding: chunked 

)

Kilian Hekhuis wrote:

Hi Anagha,

Where did you get the trace from? Is that the Integration trace from Service Center? Because it contains the request as Base64 instead of binary data (for example, a trace from a REST service we're using that also uses multipart/form-data looks like this:

POST https://my.host.nl/api/packages HTTP/1.1
User-Agent: OutSystemsPlatform
Content-Type: multipart/form-data; boundary="------9c2c9938-9667-4abe-9c98-ea7cee7359ea"
Accept: application/json; esl-api-version=11.18
Authorization: XXXXXXXX
Host: my.host.nl
Content-Length: 118528

<BINARY DATA>

HTTP/1.1 200 OK
Vary: Accept-Encoding
Transfer-Encoding: chunked 

)

 Ok Yes, Now I am getting just like the above trace from Integration service center
The earlier trace was probably from when I was trying to get the data as base64.
Below is the trace.

POST https://----------

User-Agent: OutSystemsPlatform

Content-Type: multipart/form-data; boundary="------8c60b69a-7dbe-4999-bb21-ce02e519b056"

Authorization: Basic ------

Host: ---------

Content-Length: 45852


<BINARY DATA>


HTTP/1.1 200 OK

Content-Length: 119

Cache-Control: private

Content-Type: application/json; charset=utf-8

Date: Thu, 02 Jul 2020 13:42:31 GMT

Server: Microsoft-IIS/10.0

X-AspNet-Version: 4.0.30319

X-Powered-By: ASP.NET



{"payload":{"exceptions":[{"userMessage":"Unable to parse incoming data."}],"status":{"code":"400","message":"Error"}}}

 

Ok, so we can rule that out as well as cause of this error. I must admit I'm almost out of ideas. The example OML looks good, what we can see of the data looks good too (when I decoded the Base64, it looked good as well). One last thing to check the content of the Binary Data, is to create a testing Screen, that does all the same PartAdds and MultipartFormDataCreate, and download the output of MultipartFormDataCreate, and attach that file here, to see if I can find anything out of the ordinary.