Json 使用curl发布多部分/表单数据、文件和大量键值对
作为模拟应用程序前端在后端工作时所做工作的一部分,我一直在运行各种curl命令。很容易获得一个文件作为Json 使用curl发布多部分/表单数据、文件和大量键值对,json,forms,curl,post,Json,Forms,Curl,Post,作为模拟应用程序前端在后端工作时所做工作的一部分,我一直在运行各种curl命令。很容易获得一个文件作为内容类型:application/octet-stream发送,或者只使用内容类型:application/jsonjson发送 我将json与以下内容内联发送: curl -X POST -H "Content-Type: application/json" -d '{"username":"xyz","password":"xyz"}' http://127.0.0.1:8088 从文件中
内容类型:application/octet-stream
发送,或者只使用内容类型: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
curl --request POST -H "Content-Type:application/octet-stream" --data-binary "@photo.jpg" http://127.0.0.1:8088
curl --verbose --request POST --header "Content-Type:multipart/form-data" --form key1=value1 --form upload=@photo.jpg http://127.0.0.1:8088
(任何关于“接受”功能的想法?没有它就不起作用……,解决方案来自)
仅发送一个文件,如下所示:
curl -X POST -H "Content-Type: application/json" -H 'Accept: application/json' --data-binary @test.json http://127.0.0.1:8088
curl --request POST -H "Content-Type:application/octet-stream" --data-binary "@photo.jpg" http://127.0.0.1:8088
curl --verbose --request POST --header "Content-Type:multipart/form-data" --form key1=value1 --form upload=@photo.jpg http://127.0.0.1:8088
带有json内联的Multipart和一个非常好的图片,如下所示:
curl -X POST -H "Content-Type: application/json" -H 'Accept: application/json' --data-binary @test.json http://127.0.0.1:8088
curl --request POST -H "Content-Type:application/octet-stream" --data-binary "@photo.jpg" http://127.0.0.1:8088
curl --verbose --request POST --header "Content-Type:multipart/form-data" --form key1=value1 --form upload=@photo.jpg http://127.0.0.1:8088
(解决方案来自)
当我试图从文件和照片中提取键值对,或者将json粘贴到curl命令中(curl命令也上载文件)时,问题就开始了。能否说服curl
发送“内容类型:多部分/表单数据”
,其中键值对来自文件,文件附件来自文件
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
有什么办法可以让它工作吗?我认为你的思路是正确的,但是看看
curl
手册页面可能会让你走得更远
--表单
选项文档中的一些关键要点:
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和JPEG的MIME类型
最后要记住的一点是:此选项可以多次使用。
这其中的大部分只是@Tanaike上面所说的话的一个回音,但是文档中有更多的解释。我建议你更详细地阅读它
我认为
curl
对您的命令最大的抱怨是表单的每个部分都有键upload
。这有点模棱两可。JSON有一个键,图片有一个键吗
了解Web服务器对表单中每个字段的期望也是非常重要的。文件上传和文本字段之间有很大的区别。在@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
在您的问题中,
--formkey1=value1--formupload=@photo.jpg
有效。那么这个--form“upload1=@Tanaike”怎么办?它不太管用,我想你可能用了尖括号,以前没见过。我正在玩,但还没有解决。我真的很抱歉我帮不上忙。是的,它正在工作。如果我把“@”改成a”