使用来自文件的发布数据向 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.