13

I want to send json request and several files in one http request. I'm using multipart/mixed request for that

curl -H "Content-Type: multipart/mixed" -F "request=@body.json; type=application/json" -F "file1=@2.xml" -F "file2=@2.pdf" -X POST hostName

request field has application/json content type and by that indication I define that this part is json request and other parts are files.

My question is how to inline request body in curl request. I try to use

curl -H "Content-Type: multipart/mixed" -F "request={"param1": "value1"}" -F "file1=@2.xml" -F "file2=@2.pdf" -X POST hostName

but content type of request will be plain/text

Nawa
  • 2,058
  • 8
  • 26
  • 48

2 Answers2

13

You can add content-type information after semicolon:

curl -H "Content-Type: multipart/mixed" -F "request={"param1": "value1"};type=application/json"
splatch
  • 1,547
  • 2
  • 10
  • 15
8

To attach both a payload and a file using curl command, some like this will do.

curl -i -X POST -H "Content-Type: multipart/mixed" \
-F "somepayload={\"name\":\"mypayloadname\"};type=application/json" \
-F "uploadfile=@somevalid.xml" http://localhost:8080/path/topost

Make sure you escape the payload content and somevalid.xml should be there in the same directory where curl is executed or replace it with valid path to the file.

raksja
  • 3,969
  • 5
  • 38
  • 44
  • 1
    With my curl version (7.52.1), it still valid `-H "Content-Type: multipart/form-data"`, having json content in an external file too, that is to say using `-F "info=@path/content.json;type=application/json" -F "file=@path/doc.pdf; type=application/pdf"` – Stefano Scarpanti Jul 04 '17 at 12:16