Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I rewrite this CURL multipart/form-data request without using -F?

How can I rewrite the following CURL command, so that it doesn't use the -F option, but still generates the exact same HTTP request? i.e. so that it passes the multipart/form-data in the body directly.

curl -X POST -F example=test http://localhost:3000/test 
like image 411
William Denniss Avatar asked May 26 '12 09:05

William Denniss


People also ask

What is the right way to post multipart form data using curl?

The proper way to upload files with CURL is to use -F ( — form) option, which will add enctype=”multipart/form-data” to the request.

How is multipart form data encoded?

The encoding process is performed before data is sent to the server as spaces are converted to (+) symbol and non-alphanumeric characters or special characters are converted to hexadecimal (0-9, A-F) values as the ASCII character set is the format for sending data on the Internet.

What is Multipartcontent?

7.2 The Multipart Content-Type. In the case of multiple part messages, in which one or more different sets of data are combined in a single body, a "multipart" Content-Type field must appear in the entity's header.


2 Answers

Solved:

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

Where test.txt contains the following text, and most importantly has CRLF (\r\n) line endings:

------------------------------4ebf00fbcf09 Content-Disposition: form-data; name="example"  test ------------------------------4ebf00fbcf09-- 

Notes: it is important to use --data-binary instead of plain old -d as the former preserves the line endings (which are very important). Also, note that the boundary in the body starts with an extra --.

I'm going to repeat it because it's so important, but that request-body file must have CRLF line endings. A multi-platform text editor with good line-ending support is jEdit (how to set the line endings in jEdit).

If you're interested in how I worked this out (debugging with a Ruby on Rails app) and not just the final solution, I wrote up my debugging steps on my blog.

like image 190
William Denniss Avatar answered Sep 23 '22 19:09

William Denniss


You can use the --form argument with an explicitly

curl -H "Content-Type: multipart/related" \   --form "[email protected];type=image/jpeg" http://localhost:3000/test 
like image 27
mimming Avatar answered Sep 22 '22 19:09

mimming