Tell me more ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How do I make a POST request with cURL command-line tool?

share|improve this question

migrated from stackoverflow.com Jun 6 '10 at 7:46

5 Answers

curl --data "param1=value1&param2=value2" http://example.com/resource.cgi

or

curl --form "fileupload=@filename.txt" http://example.com/resource.cgi

For more information see the cURL manual. You might also find the cURL tutorial on emulating a web browser helpful.

With libcurl, you'd use the curl_formadd() function to build your form before submitting it in the usual way. See the libcurl documentation for more information.

share|improve this answer
@LauriRanta --data-urlencode (no dash), in recent versions at least – waitinforatrain Feb 12 at 12:34
1  
--data '' works if there is no POST data, but the recipient requires a request to be POSTed. – Daniel Beck Apr 27 at 13:15

For a RESTful HTTP POST containing XML:

curl -X POST -d @filename http://example.com/path/to/resource --header "Content-Type:text/xml"

or for JSON, use this:

curl -X POST -d @filename.txt http://example.com/path/to/resource --header "Content-Type:application/json"

This will read the contents of file named filename.txt and send it as the post request.

share|improve this answer
14  
If your endpoint is expecting text/xml, append --header "Content-Type:text/xml" to your parameters. – TK Kocheran Oct 19 '11 at 0:16
1  
Could you provide some explanation about what your code does? – Tom Wijsman Nov 5 '11 at 1:36
9  
If you are sending json, you can specify the content type with: --header "Content-Type:application/json" – Juha Palomäki Mar 20 '12 at 16:13
1  
@tom-wijsman explanation: curl -X POST implies an HTTP POST request, the -d parameter (long version: --data) tells curl that what follows will be POST parameters, and @filename designates the contents of the file filename as parameter. This approach works best with RESTful HTTP APIs as found at Twitter, Facebook, various other web services including Ruby on Rails as well as HTTP APIs of databases such as CouchDB. REST stands for Representational state transfer – soundmonster Jun 27 '12 at 11:27
1  
How is this more RESTful than other ways though? – Niklas Berglund Oct 3 '12 at 15:59
curl -d "name=Rafael%20Sagula&phone=3320780" http://www.where.com/guest.cgi 

is the example found in the Curl Example Manual.

Use %26 for the ampersands though if the above doesn't work:

curl -d "name=Rafael%20Sagula%26phone=3320780" http://www.where.com/guest.cgi 
share|improve this answer

If you want to login to a site, do the following:

curl -d "username=admin&password=admin&submit=Login" --dump-header headers http://localhost/Login
curl -L -b headers http://localhost/

The first request saves the session cookie (that is provided upon successful login) in the "headers" file. From now on you can use that cookie to authenticate you to any part of the website that you usually access after logging in with a browser.

share|improve this answer
curl -v --data-ascii var=value http://example.com

and there are many more options, check curl --help for more information.

share|improve this answer

protected by studiohack May 11 '11 at 16:02

This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.