How to test multipart/form-data POST request

14,953

You can use curl for testing these libraries, here's an example using a multipart/form-data POST: https://stackoverflow.com/a/10765244/72176

One thing I like about a command-line tool like curl is it's easy to repeat (in bash, up & enter), and you can save the test for later.

Edit: It is definitely possible to send the custom header that you want to test. The key is to use curl's raw commands over the convenience methods which format the request for you. Use -H to pass in the raw header, and use --data-binary to pass in the body from a file without changing the line endings (very important for multipart/form-data which must have CRLF line endings). Here's an example:

curl -X POST -H "Content-Type: multipart/form-data; boundary=----------------------------4ebf00fbcf09" --data-binary @test.txt http://localhost:3000/test

of if it's more convenient not to use the intermediary file, you can write it one line like so:

curl -X POST -H "Content-Type: multipart/form-data; boundary=----------------------------4ebf00fbcf09" -d $'------------------------------4ebf00fbcf09\r\nContent-Disposition: form-data; name="example"\r\n\r\ntest\r\n------------------------------4ebf00fbcf09--\r\n' http://localhost:3000/test

These 2 examples include the semicolon, but you can remove it as needed.

Share:
14,953
sdoca
Author by

sdoca

Updated on June 04, 2022

Comments

  • sdoca
    sdoca almost 2 years

    I am trying to find a tool that will allow me to test a multipart/form-data POST request and tweak the request. Specifically, I want to test the absence/presence of the semi-colon in the content-type header:

    multipart/form-data; boundary=140f0f40c9f411e19b230800200c9a66
    

    We have a client that doesn't send a semi-colon and our new servlet (using Apache Commons FileUpload) can't parse the uploaded file. The old version of our servlet uses a different library method for accepting/parsing the request and it can parse the file. Until I can prove that the request will be successful by including the semi-colon, the owners of the client app don't want to make any changes to it.

    I have been using cURL to run my tests against the servlet, but I can't tweak the request it generates to exclude the semi-colon. I have tried the Poster addon for Firefox and Fiddler to generate a test POST request, but they result in this error:

    org.apache.commons.fileupload.FileUploadException: Stream ended unexpectedly
    

    Has anybody found a way to successfully test a multipart/form-data POST request with an uploaded file?