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

How can I rewrite the following CURL command so that it does not use the -F option, but still generates the same HTTP request? that is, so that it transfers multiple / form-data in the body directly.

 curl -X POST -F example=test http://localhost:3000/test 
+27
curl multipartform-data
May 26 '12 at 9:57 a.m.
source share
6 answers

It is decided:

 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 a CRLF line ending (\ r \ n) :

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

Notes: it is important to use --data-binary instead of the usual old -d , since the first saves the end of the line (which is very important). Also note that the boundary in the body begins with an extra -- .

I am going to repeat it because it is so important, but this request-body file must have a CRLF line ending. A multi-platform text editor with good end-of-line support is jEdit ( how to set line endings in jEdit ).

If you are interested in how I did this (debugging using the Ruby on Rails application), and not just the final solution, I wrote my debugging steps on my blog .

+55
May 26 '12 at 9:57 a.m.
source share

You can use the --form argument with explicitly

 curl -H "Content-Type: multipart/related" \ --form "data=@example.jpg;type=image/jpeg" http://localhost:3000/test 
+20
Dec 11 '12 at 18:56
source share

Here's an alternative answer with the original CURL operation rewritten using -d as a single line, without temporary files. Personally, I think the temporary file approach is easier to understand, but I also put it here for reference:

 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 

Notes: $'blar' syntax is such that bash will parse \ r \ n as a CRLF token. Thanks for this answer .

+11
May 26 '12 at 10:30
source share

This is what I use, I think it is clean and does not need temporary files and does not consume RAM if you want to load whole files (therefore there is no reading of files into memory).

 # Set these two. file='path/to/yourfile.ext' url='http://endpoint.example.com/foo/bar' delim="-----MultipartDelimeter$$$RANDOM$RANDOM$RANDOM" nl=$'\r\n' mime="$(file -b --mime-type "$file")" # This is the "body" of the request. data() { # Also make sure to set the fields you need. printf %s "--$delim${nl}Content-Disposition: form-data; name=\"userfile\"${nl}Content-Type: $mime$nl$nl" cat "$file" printf %s "$nl--$delim--$nl" } # You can later grep this, or something. response="$(data | curl -# "$url" -H "content-type: multipart/form-data; boundary=$delim" --data-binary @-)" 
+2
Feb 24 '16 at 8:24
source share

This is loading a single image file using "Content-Type: multipart / related",

 curl --trace trace.txt -X POST -H 'Content-Type: multipart/related; boundary=boundary_1234' --data-binary $'--boundary_1234\r\nContent-Type: application/json; charset=UTF-8\r\n\r\n{\r\n\t"title": "TestFile"\r\n}\r\n\r\n--boundary_1234\r\nContent-Type: image/jpeg\r\n\r\n' --data-binary '@Image0177.jpg' --data-binary $'\r\n--boundary_1234--\r\n' 'http://localhost:3000/google/upload/drive/v2/files?uploadType=multipart' 
0
Apr 03 '14 at 5:47
source share

This is for the multipart / form-data request method. to load the file add -form filename = "@ path / image.jpg; type = image / jpeg"

curl --form key = "value" - form key = "value" http: // localhost: 3000 / test

-3
Jan 19 '16 at 11:01
source share



All Articles