使用来自文件的发布数据向 cURL 发送请求 [英] Send request to cURL with post data sourced from a file

查看:34
本文介绍了使用来自文件的发布数据向 cURL 发送请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从命令行通过 cURL 发出 POST 请求.此请求的数据位于文件中.我知道通过 PUT 这可以通过 --upload-file 选项来完成.

I need to make a POST request via cURL from the command line. Data for this request is located in a file. I know that via PUT this could be done with the --upload-file option.

curl host:port/post-file -H "Content-Type: text/xml" --data "contents_of_file"

推荐答案

您正在寻找 --data-binary 参数:

curl -i -X POST host:port/post-file 
  -H "Content-Type: text/xml" 
  --data-binary "@path/to/file"

在上面的例子中,-i 打印出所有的标题以便你可以看到发生了什么,并且 -X POST 明确表示这是一个帖子.这两者都可以安全地省略而不会改变线路上的行为.文件路径需要以 @ 符号开头,以便 curl 知道从文件中读取.

In the example above, -i prints out all the headers so that you can see what's going on, and -X POST makes it explicit that this is a post. Both of these can be safely omitted without changing the behaviour on the wire. The path to the file needs to be preceded by an @ symbol, so curl knows to read from a file.

这篇关于使用来自文件的发布数据向 cURL 发送请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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