Set the content type of a part of a multipart / mixed request to CURL

I want to send a json request and multiple files in one HTTP request. I am using multipart/mixed request for this

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

request is of the content type application/json , and by this sign I determine that this part is a json request, and the other parts are files.

My question is how to embed the request body in a curl request. I'm trying to use

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

but the request content type will be plain/text

+5
source share
2 answers

After the semicolon, you can add content type information:

 curl -H "Content-Type: multipart/mixed" -F "request={"param1": "value1"};type=application/json" 
+8
source

To attach both the payload and the file using the curl command, some of them 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 that you avoid the contents of the payload, and somevalid.xml should be in the same directory where the curl is executed, or replace it with a valid path to the file.

+4
source

Source: https://habr.com/ru/post/1208055/


All Articles