What is the cURL command-line syntax to do a POST request?

With fields:

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

With fields specified individually:

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

Multipart:

curl --form "[email protected]" https://example.com/resource.cgi

Multipart with fields and a filename:

curl --form "[email protected];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.


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.


Data from stdin with -d @-

Example:

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

Output:

<p>Hello <strong>world</strong>!</p>

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 

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.