作为模拟应用程序前端在我后端工作时的行为的一部分,我一直在运行各种curl命令。通过
我使用以下内容来内联发送json:
从文件中提取json并以这种方式发送:
(有什么想法关于“接受”是做什么的吗?没有它就不能工作...,解决方案来自这里)
只是像这样单独发送一个文件:
(来自这里的解决方案)
当我尝试从文件中提取键值对和图片,或者将json粘贴到同时上传文件的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
(有什么想法关于“接受”是做什么的吗?没有它就不能工作...,解决方案来自这里)
只是像这样单独发送一个文件:
curl --request POST -H "Content-Type:application/octet-stream" --data-binary "@photo.jpg" http://127.0.0.1:8088
使用内联json和图片的多部分内容可以像这样非常好地进行:
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
用来自文件的键值对和来自文件的文件附件发送"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