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
curl --form key="value" --form key="value" http://localhost:3000/test
Here it is how I would do it:
Or a bit more sophisticated (should be portable to most modern shells):
You can use the
--form
argument with an explicitlyThis is to upload one image file using "Content-Type: multipart/related",
This is what I'm using, I think it's clean and doesn't need temporary files nor gobbles up RAM in case you want to upload whole files (so no reading files into memory).
Solved:
Where
test.txt
contains the following text, and most importantly has CRLF (\r\n) line endings: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.