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

前端 未结 4 1183
别那么骄傲
别那么骄傲 2021-02-19 03:30

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

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-19 03:40

    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\":\"john.smith@gmail.com\"};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.

提交回复
热议问题