Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test multipart/form-data POST request

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?

like image 282
sdoca Avatar asked Jul 11 '12 17:07

sdoca


People also ask

How do I pass a multipart file in Junit?

I have done the multipart file as a input for junit by using [MockMultipartFile]. FileInputStream inputFile = new FileInputStream( "path of the file"); MockMultipartFile file = new MockMultipartFile("file", "NameOfTheFile", "multipart/form-data", inputFile); now use the file input as multipart file. Save this answer.

How can I pass Formdata in Postman?

POST request to send a form (multipart/form-data) To send a POST request, select the POST request method, click on Body, and select form-data. If you look at the response body, you will notice that the data you have submitted.


1 Answers

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.

like image 183
William Denniss Avatar answered Oct 22 '22 15:10

William Denniss