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

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question

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

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

up vote 1710 down vote
+50

With fields:

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

Multipart:

curl --form "fileupload=@my-file.txt" https://example.com/resource.cgi

Multipart with fields and a filename:

curl --form "fileupload=@my-file.txt;filename=desired-filename.txt" --form param1=value1 --form param2=value2 https://example.com/resource.cgi

Without data:

curl --data '' https://example.com/resource.cgi

curl -X POST https://example.com/resource.cgi

curl --request POST https://example.com/resource.cgi

For more information see the cURL manual. The cURL tutorial on emulating a web browser is helpful.

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

For large files, consider adding parameters to show upload progress:

curl --tr-encoding -X POST -v -# -o output -T filename.dat \
  http://example.com/resource.cgi

The -o output is required, otherwise no progress bar will appear.

share|improve this answer
3  
@LauriRanta --data-urlencode (no dash), in recent versions at least – waitinforatrain Feb 12 '13 at 12:34
1  
Also works if you need to update a resource with a PUT: curl -X PUT ... – Subfuzion Jan 22 '14 at 4:38
3  
I'm having trouble understanding... when would I do it With Fields, when with Multipart and when Without Data? – CodyBugstein Sep 21 '14 at 11:05
1  
Instead of --data you can use -d. – user35538 Oct 9 '15 at 16:32
    
i have an array of fields. how can i do this? – ARUNBALAN NV Mar 9 at 13:13

For a RESTful HTTP POST containing XML:

curl -X POST -d @filename.txt 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 the file named filename.txt and send it as the post request.

share|improve this answer
7  
@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

Data from stdin: use -d @-. Example:

echo '{"text": "Hello **world**!"}' | curl -d @- https://api.github.com/markdown

Output:

<p>Hello <strong>world</strong>!</p>
share|improve this answer
1  
+1 for interesting example – Brian Peterson May 23 at 6:46
1  
Great if you have a JSON object already in clipboard – Luca Steeb 8 hours ago
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
3  
a note from curl's man page: 'The -c, --cookie-jar option is however a better way to store cookies.' – maxschlepzig Dec 28 '13 at 15:14
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

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?