59
Views
18
Comments
Solved
[Multipart/form-data] Expose API issue with multipart/form
multipart-form-data
Service icon
Forge component by Kilian Hekhuis
Application Type
Service
Service Studio Version
11.54.27 (Build 62831)
Platform Version
11.23.1 (Build 40847)

I have exposed an API for uploading PDF files via a POST method using REST as shown below:


I also use the MultipartFormData forge to handle 'multipart/form-data' from requests like this




But when I try to test this API in Postman, it shows the error:

 "The request entity's media type 'multipart/form-data' is not supported for this resource."

I don't understand why this error occurs when I've already handled the multipart form in the request. 

What should I do now? I want to find a solution that allows me to submit both the file name and binary fields in a single object request.

TestExposeAPI.oml
Solution

The goal of a MultiPart-form data endpoint is to have the ability to send multiple data at once, like a binary file as a part and that file metadata as another part for example as a JSON.

Also, as mentioned by @Kilian Hekhuis, the "file part" already receives the file name, so it is just a matter of parsing it.
If you need additional information, just send it as an additional part. That "part", could be a JSON with all the fields you require, e.g. InvoiceNO, OrderID, etc..).

You would then receive that JSON part, parse it, and associate the file to that particular Order/Invoice.

Hi Phuong,

I don't know this error. Did you check the error log in Service Center? It's important to know at what stage this error is generated.

It shows error in my service center 

Hi Phuong,

I think you missed passing Content-Type in the Header of your Postman. I believe you already gone through with provided demo. You can try to run with Content type as given in SS.


I hope this helps!

I see it auto-generated by Postman , I have tried it before I post this question but it doesn't work for me


Hi @Phuong Nguyen Truc Dong ,

Did you check the demo component's implementation?
Looking at your exposed API, you are not receiving a Binary variable. Instead, you have a structure with 2 attributes that the platform will try to parse as JSON:


In the demo component, you can see that it is only receiving a single attribute of type binary received in the body:


Then, you can use the same logic you had before to parse the MultiPart-FormData content as you did, or use logic similar to the one on the demo:



Regards,

Hi Barata, I checked  the demo component's implementation and the "Binary" field in my structure is binary type .
And the reason I don't have a for loop like the demo is because I only parse a single file from the request object 

Hi Phuong,

You are doing it wrong. Like Joao, wrote, you need Binary Data as single input, as a multipart/form-data payload is a single binary object. You have a structure as input, which is quite different from multipart/form-data. In fact, like Joao wrote, your request is interpreted as JSON, so your REST service expects something like:

   {
      "Binary":"VGhpcyBpcyBhbiBleGFtcGxlIG9mIEJhc2U2NCBlbmNvZGVkIHRleHQu",
      "FileName":"Myfile.txt"
   }

Which, again, is not multipart/form-data.

Hi @Phuong, Please find the updated OML and reference SS for the postman.



Thanks,

Narendra

TestExposeAPI.oml

I'm tested and it works for me. So I cannot put the field file name and binary together in the API body. My API body only contains binary, the other will in URL params, is that true?

Although I did not try on my end, I think it won't work as it has media type input, but you can try at your end.

If you believe your problem has been resolved, mark it as such.

No Phuong,

That's not true. Multipart/form-data includes the filename, and the binary of the file, for one or multiple files, in a single binary input, as explained above.


Tks, Narendra. However, I want to explore other solutions from different people because I want to put all the fields into a single request object . And  I want those who don't know to gain more knowledge when they read this article. I will close this post as soon as I find the most suitable solutions 


But I want to add more fields not related to the binary file, for example, fields like Invoice No and Order Id , UserId.... and put it all into the single request object in body of request. Can I do that? 

Solution

The goal of a MultiPart-form data endpoint is to have the ability to send multiple data at once, like a binary file as a part and that file metadata as another part for example as a JSON.

Also, as mentioned by @Kilian Hekhuis, the "file part" already receives the file name, so it is just a matter of parsing it.
If you need additional information, just send it as an additional part. That "part", could be a JSON with all the fields you require, e.g. InvoiceNO, OrderID, etc..).

You would then receive that JSON part, parse it, and associate the file to that particular Order/Invoice.

Champion

Hi @Phuong Nguyen Truc Dong ,

Can you please try this component.

https://www.outsystems.com/forge/component-documentation/9292/generic-connector-for-multipart-form-data-request/0

It is easy to use no need to create REST API. you can call direct using extension.

Dynamic Parameter also support.

Hope this will help you :)

Let me know if you need any further help :)

Thanks,

AV


Amit, please read carefully. Phuong is exposing a REST service that is multipart/form-data (or at least, he's trying), not consuming one. You can't expose REST services via an extension.

Tks, Amit, but in this case, I want to expose the API that accepts binary files and other fields in a single request object. 

Champion

Ok...will check and let me know if anything found regarding this.

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.