Send POST Request with Data Specified in File via

2019-01-08 04:24发布

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"

4条回答
混吃等死
2楼-- · 2019-01-08 05:04

You're looking for the --data-binary argument:

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

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.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-01-08 05:06

Most of answers are perfect here, but when I landed here for my particular problem, I have to upload binary file(XLSX spread sheet) using POST, I see one thing missing, i.e. usually its not just file you load, you may have more form data elements, like comment to file or tags to file etc as was my case. Hence, I would like to add it here as it was my use case, so that it could help others.

curl POST -F comment=mycomment -F file_type=XLSX -F file_data=@/your/path/to/file.XLSX http://yourhost.example.com/api/example_url
查看更多
疯言疯语
4楼-- · 2019-01-08 05:11

I need to make a POST request via Curl from the command line. Data for this request is located in a file...

All you need to do is have the --data argument start with a @:

curl -H "Content-Type: text/xml" --data "@path_of_file" host:port/post-file -path

For example, if you have the data in a file called stuff.xml then you would do something like:

curl -H "Content-Type: text/xml" --data "@stuff.xml" host:port/post-file-path

The stuff.xml filename can be replaced with a relative or full path to the file: @../xml/stuff.xml, @/var/tmp/stuff.xml, ...

查看更多
祖国的老花朵
5楼-- · 2019-01-08 05:12

If you are using form data to upload file,in which a parameter name must be specified , you can use:

curl -X POST -i -F parametername=@filename host:port/xxx

查看更多
登录 后发表回答