curl - 如何编写具有命名多部分主体的 curl

标签 curl multipart

我有一个接受多部分文件的资源,它看起来像这样:

@FormDataParam("file") InputStream inputStream

我想向该资源写入curl,但我不知道如何将此参数命名为"file"。

你有什么想法吗?

最佳答案

使用-F切换到多部分发送。当您至少使用此开关时,Content-Type 将自动设置为 multipart/form-data。您可以拥有多个。对于每一个,您可以设置值 <name>=<value><value>可以是一个文件,使用 @在值前面。例如-F "file=@path_to_file" 。来自the manpage

-F, --form <name=content>

(HTTP) This lets curl emulate a filled-in form in which a user has pressed the submit button. This causes curl to POST data using the Content-Type multipart/form-data according to RFC 2388. This enables uploading of binary files etc. To force the 'content' part to be a file, prefix the file name with an @ sign. To just get the content part from a file, prefix the file name with the symbol <. 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.

Example: to send an image to a server, where 'profile' is the name of the form-field to which portrait.jpg will be the input:

curl -F <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2d5d5f424b444148106d5d425f595f4c445903475d4a" rel="noreferrer noopener nofollow">[email protected]</a> https://example.com/upload.cgi

To read content from stdin instead of a file, use - as the filename. This goes for both @ and < constructs. Unfortunately it does not support reading the file from a named pipe or similar, as it needs the full size before the transfer starts.

You can also tell curl what Content-Type to use by using 'type=', in a manner similar to:

curl -F "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="88ffedeab5c8e1e6ecedf0a6e0fce5e4" rel="noreferrer noopener nofollow">[email protected]</a>;type=text/html" example.com

or

curl -F "name=daniel;type=text/foo" example.com

You can also explicitly change the name field of a file upload part by setting filename=, like this:

curl -F "file=@localfile;filename=nameinpost" example.com

If filename/path contains ',' or ';', it must be quoted by double-quotes like:

curl -F "file=@\"localfile\";filename=\"nameinpost\"" example.com

or

curl -F 'file=@"localfile";filename="nameinpost"' example.com

Note that if a filename/path is quoted by double-quotes, any double-quote or backslash within the filename must be escaped by backslash.

See further examples and details in the MANUAL.

This option can be used multiple times.

关于curl - 如何编写具有命名多部分主体的 curl ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37210093/

相关文章:

http - 将 PUT HTTP 请求代理到 AWS S3 失败

java - 如何使用 JavaMail 处理多部分/替代邮件?

python - 帮助改进我的文件上传方法( Pyramid 框架)

ruby-on-rails - 使用curl使用摘要测试基本的HTTP身份验证

docker - 如何解决crontab中的 "curl: (7) Failed to connect to localhost port 5001: Connection refused"?

php - 如何确保在生成 PDF 之前加载图像?

java - WSO2 ESB 不会将多部分文件转发到代理服务

java - 有没有从 HttpServletRequest 读取表单数据的方法?

java - 所需的请求部分 'file' 不存在。尝试上传图像, Angular -> Spring

c++ - libcurl示例代码出现错误