[Multipart/form-data] Sending File to REST API returns Bad Request
multipart-form-data
Service icon
Forge component 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

mvp_badge
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?

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

mvp_badge
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?

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.

mvp_badge
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.

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.

mvp_badge
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.)

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

mvp_badge
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.

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.

mvp_badge
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.

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