Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send request to cURL with post data sourced from a 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" 
like image 394
breedish Avatar asked Jun 20 '11 09:06

breedish


People also ask

How do I post a request body with Curl?

To post data in the body of a request message using Curl, you need to pass the data to Curl using the -d or --data command line switch. The Content-Type header indicates the data type in the body of the request message.

Does Curl use Get or post?

GET is used by default with curl requests. If you use curl to make HTTP requests other than GET, you need to specify the HTTP method.


1 Answers

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.

like image 158
Richard J Avatar answered Oct 19 '22 09:10

Richard J