json - 如何将文件和 JSON 数据与 Curl 一起发布?

标签 json post file-upload curl curl-commandline

我一直在用这个 curl 命令发布一个文件:

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

现在我想随文件一起发送有关文件的一些信息(作为 JSON)。
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 对这种完全不正确的使用方式非常不满,在这种情况下,它说“您只能选择一个 HTTP 请求!”好的,足够公平,那么我如何将文件附件和那些 POST 变量放入单个 curl HTTP 请求中?

最佳答案

我已经成功开发了类似的端点,这些端点接受多个文件及其 JSON 格式的元数据。

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/

请注意在元数据请求部分的末尾添加了 ;type=application/json。上传多个不同类型的文件时,可以在-F值的末尾定义mime类型。

我已经确认这适用于使用@RequestPart 的 Spring MVC 4.3.7。该实例中的关键是不要在 @RequestMapping 注释上提供消耗值。

关于json - 如何将文件和 JSON 数据与 Curl 一起发布?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21948243/

相关文章:

javascript - 我如何 for...in 循环遍历这个包含多个 cookie 的 JSON 数组来获取键值对?

java - NoClassDefFound错误: org/apache/commons/io/output/DeferredFileOutputStream in uploading file with SpringMVC/Wildfly/CentOS

php - Codeigniter 文件路径未正确存储在数据库中

json - HttpClient 不使用带有俄语字符的 json 发送请求

c# - 无法使用 JavaScriptDateTimeConverter 解析数据

javascript - 如何重用 JSON 对象

image - 带有图像文件内容的 Apache 基准测试 POST

php - 使用 $_POST 变量重定向

javascript - POST 请求中的查询字符串与 json 数据

jquery - blueimp 文件上传插件 IE 问题