使用curl来发布多部分/表单数据,文件和许多键值对 [英] use curl to POST multipart/form-data, file and lots of key-value pairs

查看:264
本文介绍了使用curl来发布多部分/表单数据,文件和许多键值对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为模拟在后端工作时应用程序前端将执行的操作的一部分,我一直在运行各种curl命令.只需将文件作为Content-Type:application/octet-stream或使用Content-Type:application/json

As part of simulating what the front-end of an application will do while I work on the backend, I have been running various curl commands. Was easy to get just a file to be sent as Content-Type:application/octet-stream or just json with Content-Type:application/json

我与此发送了json内联

I sent json inline with this:

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

将json从文件中拉出并发送,如下所示:

Pulled the json out of a file and sent it like this:

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

(没有接受"功能的任何想法,如果没有它,..,此处的解决方案)

(any ideas what the 'Accept' does? does not work without it.., solution from here)

仅发送一个文件,如下所示:

Sent just a file by itself like this:

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

包含json内联和图片的多部分内容非常像这样:

Multipart with json inline and a picture goes very nicely like this:

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"?

The trouble starts when I try to pull both the key-value pairs from a file and the photo, or to paste json into the curl command which also uploads a file. Can curl be convinced to send "Content-Type:multipart/form-data" with key value pairs coming from a file and a file attachment coming from a file?

john.json

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

john.jpg

我已经尝试了一些方法,但这只会给我错误消息:

I have tried some things but it just gives me error messages:

尝试内联,粘贴到json中:

Tried inline, pasting in 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).

然后,我尝试从文件中同时获取它们,但两者都不喜欢:

Then I tried to get them both from a file, but it didn't like that either:

$ 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

有什么想法可以使这项工作成功吗?

Any ideas how to make this work?

推荐答案

我认为您走在正确的轨道上,但是看看curl手册页可能会更进一步.

I think you're on the right track, but taking a look at the curl manual page might get you further.

某些键不包含在--form选项文档中:

Some key take aways from the --form option documentation:

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,请使用<,对于图片,请使用@.

So for the JSON use <, but for the picture use @.

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

I think also you should specify the content type of each section to help the web server know how to parse each section. Although it probably has expectations for each field.

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和jpegs的MIME类型.

You'll have to look up the MIME types for JSON and jpegs.

然后要记住的最后一点:This option can be used multiple times.

And then the final piece to keep in mind: This option can be used multiple times.

大多数内容只是@Tanaike在上面所说的内容的回声,但文档中有更多解释.我建议您进一步阅读.

Most of this is just an echo of what @Tanaike is saying above, but with more explanation from the documentation. I would recommend reading it in further detail.

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

I think the largest complain curl has with your command is that each part of the form has the key upload. That's sort of ambiguous. Is there one key for JSON and one key for the picture?

了解网络服务器对表单中每个字段的期望也非常重要.文件上传和文本字段之间存在很大差异.

It's also really important to know what the webserver expects from each field in the form. There's a wide difference between a file upload and a text field.

这篇关于使用curl来发布多部分/表单数据,文件和许多键值对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆