【问题标题】:use curl to POST multipart/form-data, file and lots of key-value pairs使用 curl 发布多部分/表单数据、文件和大量键值对
【发布时间】:2019-05-12 10:38:11
【问题描述】:

作为模拟应用程序前端在我处理后端时会做什么的一部分,我一直在运行各种 curl 命令。很容易得到一个文件以Content-Type:application/octet-stream 发送或者只是带有Content-Type:application/json 的json

我用这个内联发送了 json:

curl -X POST -H "Content-Type: application/json" -d '{"username":"xyz","password":"xyz"}' http://127.0.0.1:8088

从文件中提取 json 并像这样发送:

curl -X POST -H "Content-Type: application/json" -H 'Accept: application/json' --data-binary @test.json http://127.0.0.1:8088

(任何想法'Accept'做什么?没有它就无法工作..,来自here的解决方案)

像这样单独发送一个文件:

curl --request POST -H "Content-Type:application/octet-stream"  --data-binary "@photo.jpg"  http://127.0.0.1:8088

带有json inline 的Multipart 和一张图片很像这样:

curl --verbose --request POST --header "Content-Type:multipart/form-data" --form key1=value1  --form upload=@photo.jpg   http://127.0.0.1:8088

(来自here的解决方案)

当我尝试从文件和照片中提取键值对或将 json 粘贴到同时上传文件的 curl 命令时,问题就开始了。可以说服curl 发送带有来自文件来自文件的文件附件的键值对的"Content-Type:multipart/form-data"

john.json

{
  "surname" : "Doe",
  "name" : "John",
  "city" : "Manchester",
  "address" : "5 Main Street",
  "hobbies" : ["painting","lawnbowls"]
}

john.jpg

我尝试了一些方法,但它只是给我错误消息:

试过内联,在json中粘贴:

$ curl --verbose --request POST --header "Content-Type:multipart/form-data" --data '{"surname" : "Doe","name" : "John","city" : "Manchester","address" : "5 Main Street", "hobbies" : ["painting","lawnbowls"]}'  --form upload=@john.jpg   http://127.0.0.1:8088
Warning: You can only select one HTTP request method! You asked for both POST 
Warning: (-d, --data) and multipart formpost (-F, --form).

然后我试图从一个文件中获取它们,但它也不喜欢这样:

$ curl --verbose --request POST --header "Content-Type:multipart/form-data" --form upload@john.json  --form upload=@john.jpg   http://127.0.0.1:8088
Warning: Illegally formatted input field!
curl: option --form: is badly used here
curl: try 'curl --help' or 'curl --manual' for more information

任何想法如何使这项工作?

【问题讨论】:

  • 在您的问题中,--form key1=value1 --form upload=@photo.jpg 有效。那么这个--form "upload1=<john.json" --form "upload=@photo.jpg" 怎么样?而且在您的情况下,可能不需要--header "Content-Type:multipart/form-data"。因为我无法对此进行测试,而且我不确定这是否适合您的情况,所以我在这里发表了评论。如果这不起作用,我很抱歉。
  • @Tanaike 不太好用,以为你可能已经用尖括号做了一些事情,以前没见过。我正在玩the solution in this commentthis answer,但还没有解决。
  • 真的很抱歉帮不上忙。

标签: json forms curl post


【解决方案1】:

我认为您走在正确的轨道上,但查看 curl 手册页可能会让您走得更远。

--form 选项文档中的一些关键内容:

The difference between @ and < is then that @ makes a  file  get
attached in the post as a file upload, while the < makes a text 
field and just get the contents for that text field from a file.

所以对于 JSON 使用 &lt;,但对于图片使用 @

我认为您还应该指定每个部分的内容类型,以帮助 Web 服务器知道如何解析每个部分。尽管它可能对每个领域都有期望。

You can also tell curl what Content-Type to use by using 'type=', in a manner similar to:

curl -F "web=@index.html;type=text/html" example.com

您必须查找 JSON 和 jpeg 的 MIME 类型。

最后要记住的部分:This option can be used multiple times.

大部分内容只是对@Tanaike 上面所说的内容的回应,但文档中有更多解释。我建议您更详细地阅读它。


我认为curl 对您的命令最大的抱怨是表单的每个部分都有键upload。这有点模棱两可。 JSON一键,图片一键吗?

了解网络服务器对表单中每个字段的期望也是非常重要的。文件上传和文本字段之间有很大的区别。

【讨论】:

  • 是的,它现在正在工作。如果我将一个“@”更改为“
【解决方案2】:

在@Breedly 和@Tanaike 的帮助下,下面的命令开始工作,也许有一天有人会发现它很有用:

curl --verbose --request POST --header "Content-Type:multipart/form-data" --form "upload1=<john.json" --form "upload2=@john.jpg"  http://127.0.0.1:8088

很高兴"Content-Type:multipart/form-data" 在这种情况下只覆盖他们两个。然而,它真的想要 json 的“

以下也有效:

curl --verbose --request POST  --form "upload1=<john.json;type=application/json" --form "upload2=@john.jpg;type=multipart/form-data"  http://127.0.0.1:8088

【讨论】:

    猜你喜欢
    • 2022-11-11
    • 2013-04-12
    • 1970-01-01
    • 2012-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多