且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用curl来发布多部分/表单数据,文件和许多键值对

更新时间:2023-02-24 16:00:29

我认为您走在正确的轨道上,但是看看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.