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
The proper way to upload files with CURL is to use -F ( — form) option, which will add enctype=”multipart/form-data” to the request.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With