How Can I Post Files and JSON Data Together With Curl?
Asked Answered
A

4

23

I've been posting a file with this curl command:

curl -i -F file=@./File.xlsm -F name=file -X POST http://example.com/new_file/

Now I want to send some information about the file (as JSON) along with the file.

curl -i -H "Content-Type: application/json" -d '{"metadata": {"comment": "Submitting a new data set.", "current": false }, "sheet": 1, "row": 7 }' -F file=@./File.xlsm -F name=file http://example.com/new_file/

Curl is very grumpy about being used in this completely incorrect way, and in this case it says "You can only select one HTTP request!" OK, fair enough, so how do I get the file attachment and those POST variables into a single curl HTTP request?

Aristippus answered 22/2, 2014 at 1:8 Comment(2)
Sounds like curl already told you the answer...your HTTP request has a content type of json, not multipart/form-data--it can't include a file AFAIK. If you're sending metadata about the file, that should probably be sent as HTTP request headers.Aerodynamics
Would like to accept this as the answer, but you can't do that on comments.Aristippus
U
23

I've had success developing similar endpoints that accept multiple files along with their metadata in JSON format.

curl -i -X POST -H "Content-Type: multipart/mixed" -F "blob=@/Users/username/Documents/bio.jpg" -F "metadata={\"edipi\":123456789,\"firstName\":\"John\",\"lastName\":\"Smith\",\"email\":\"[email protected]\"};type=application/json" http://localhost:8080/api/v1/user/

Notice the addition of ;type=application/json at the end of the metadata request part. When uploading multiple files of different types, you can define the mime type at the end of the -F value.

I have confirmed that this works for Spring MVC 4.3.7 using @RequestPart. The key in that instance is to not provide the consumes value on the @RequestMapping annotation.

Unhallowed answered 11/3, 2017 at 4:36 Comment(1)
Thanks - the addition of ;type=application/json worked for me. Any idea how that would be done via PHP cURL? :)Eastman
H
7

You could just add another form field:

curl -X POST http://someurl/someresource -F upload=@/path/to/some/file -F data="{\"test\":\"test\"}"

Note: due to the content type, this does not really equate to sending json to the web service.

Harrelson answered 8/6, 2015 at 15:42 Comment(0)
S
3

This worked for me:

curl -v -H "Content-Type:multipart/form-data" -F "meta-data=@C:\Users\saurabh.sharma\Desktop\test.json;type=application/json" -F "file-data=@C:\Users\saurabh.sharma\Pictures\Saved Pictures\windows_70-wallpaper.jpg" http://localhost:7002/test/upload

test.json has the json data I want to send.

Sabian answered 30/7, 2017 at 18:43 Comment(0)
P
0

From @nbrooks comment, adding an additional header HTTP header works fine as shown below by using several -H or --header flags to your curl command:


curl -H "comment: Submitting a new data set." -H  "current: false" -H "sheet: 1" -H "row: 7" -F file=@./File.xlsm -F name=file http://example.com/new_file/

comment and current can be combined into "metadata" in the request.headers processing part on the flask web server.

Purgative answered 10/7, 2020 at 0:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.