266
Views
14
Comments
[Multipart/form-data] Sending File to REST API returns Bad Request
multipart-form-data
Service icon
Forge asset by Kilian Hekhuis
Application Type
Service

Hi,


So I'm sending an image file to a REST API using the multipart/form-data forge component and the response is bad request with the message "No file was submitted". This is a reactive web application.

Logic flow:

The AddImage server action, The File variable is an input variable which has the binary data from the upload widget from the UI:

The error message:

Can anyone please provide some guidance? I have also tried the above without sending the Accept and Content-Disposition headers. Same issue.


Thanks,

Abhishek

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Hi Abishek,

It's difficult to know what's wrong without knowing the API specification for this REST API. Often the API needs a specific Name for the part, it seems you named it "TestPart", which may well not work. Also, a filename might be required, are you sure the "FIlename" input is filled?

2024-03-04 06-26-43
Abhishek Puranik

Hi Kilian,

Thank you for replying.

I can confirm the filename has the name of the file

The below is from the swagger that I used to test the API:


Please let me know if you need more information.

Regards,

Abhishek

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

I see now you have the "Content-Disposition" header set to "form-data" in the PostUpload method. Are you sure this is correct? I wouldn't expect a Content-Disposition at all (also isn't present in the curl example). What happens if you remove it?

2024-03-04 06-26-43
Abhishek Puranik

I added the "Accept" and "Content-Disposition" headers after  I tested it initially. I was getting the same bad request error before adding those 2 headers. Those 2 headers didn't do anything. I am also not sure what to pass for the Name of the Part since nothing is mentioned in the swagger.

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

You may try to save the output of MultipartFormDataCreate (the MessageBinary output) to a file, and inspect the content to see what message is being sent. If possible, you could post it (or the relevant parts) here.

2024-03-04 06-26-43
Abhishek Puranik

I tried saving the MessageBinary output into a file(I'm not sure what extension to use so I didnt add it). It downloaded an empty text file where the size was 0KB. Do you have any idea why this happened? Please let me know if I was supposed to add a specific extension.

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Just to make sure the binary isn't actually zero (I wouldn't expect it to be), could you add a check on the binary size (using BinaryDataSize from the BinaryData extension) and check in debug what the size is? (Note extension shouldn't make any difference when saving the file.)

2024-03-04 06-26-43
Abhishek Puranik

So InputBinarySize is the size of the File variable and MultipartBinarySize is the size of the MessageBinary output.

Apologies, I just realized there was error which caused it to not pass the binary data back for download. After fixing the error I was able to download the file which I will attach in this message.(I added the txt extension after download since i couldn't attach a file without extension here)

MessageBinaryOutputTxt.txt
2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

I don't see anything strange there, this looks as expected:

--------197724bb-0e93-4844-a7a9-172d0430358c
Content-Disposition: form-data; name="TestPart"; filename="profileimg.png"
Content-Type: image/png

So I'd tentively conclude that this part is ok. However, in the curl example you posted earlier there's an X-CSRFToken sent along as header - is that a requirement you skipped? Because it's neither in your code nor in the dump from Service Center, so I assume it's not sent.

2024-03-04 06-26-43
Abhishek Puranik

I have tried another API and I'm not passing that token. It is working just fine. Also I think that token is only passed during AJAX requests.

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Mmm, ok. Then I'm frankly out of ideas. Everything seems like it should work. The only thing I can still think of is that the service doesn't accept the fact you say you send it a PNG, but it's not actually a PNG. The binary you attached shows the header of the file is:

RIFF~4  WEBPVP8

Which is not a PNG header, but a WebP header. A PNG file should start with:

ëPNG♪◙→◙

So you're basically uploading something that's not a PNG and saying it is a PNG. So try uploading a real PNG, and see how that goes.

2018-06-21 09-31-24
Tiago Bojikian Costa Vital

Hi team

I'm also experiencing bad requests using this component in my REST service to send binary files in the request, but the strangest thing is that the error only occurs in the PROD environment. The same application with the same data, scenario, in DEV and TST doesn't have any errors.

Do you know if there's anything in the production environment that could be affecting the request?

Thanks

Tiago Vital

2018-06-21 09-31-24
Tiago Bojikian Costa Vital

Just to update, the issue was related to a UTF8 being applied to the request in the onBeforeRequest.

I just don't understand why these issues only occur in production. In all other environments, this wasn't an issue.

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

It's impossible to tell without a lot more information why it only occurred in production. My guess would be that it was by pure chance, the conversion transforming the binary data into something that wasn't accepted in production, while not doing so in test.

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