【发布时间】: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 comment 和this answer,但还没有解决。
-
真的很抱歉帮不上忙。