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

link|improve this question
feedback

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

This question came from our site for professional and enthusiast programmers.

5 Answers

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

or

curl -F "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.

link|improve this answer
6  
-d = --data and -F = --form. You can also encode parameters with curl --data-url-encode. – Lri Nov 21 '11 at 16:13
feedback

For a RESTful HTTP POST containing XML or JSON, use this:

curl -X POST -d @filename http://example.com/path/to/resource

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

link|improve this answer
3  
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
2  
If you are sending json, you can specify the content type with: --header "Content-Type:application/json" – Juha Palomäki Mar 20 at 16:13
feedback

curl -d "name=Rafael%20Sagula&phone=3320780" http://www.where.com/guest.cgi is the example found in the Curl Example Manual that can be found here : http://www.cs.sunysb.edu/documentation/curl/index.html

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

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

link|improve this answer
feedback

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.

link|improve this answer
feedback

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.