- send an empty post request
curl -X POST https://catonmat.net
- send a post request with form data
curl -d 'login=emma&password=123' -X POST https://google.com/login
curl -d 'login=emma&password=123' https://google.com/login
curl -d 'login=emma' -d 'password=123' https://google.com/login
curl -d 'name=johnny%20depp' https://google.com/login
Content-Type=application/x-www-form-urlencoded
- send a post request and follow a redirect
curl -L -d 'tweet=hi' https://api.twitter.com/tweet
this recipe uses the -L command line option that tells curl to follow any possible redirects that it may encounter in the way. By default, curl doesn`t follow the redirects, so you have to and -L to make it follow them.
- send a post request with json data
curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json' https://google.com/login
application/json
- send a post request with xml data
curl -d '<user><login>ann</login><password>123</password></user>' -H 'Content-Type: text/xml' https://google.com/login
text/xml
- send a post request with plain text data
curl -d 'hello world' -H 'Content-Type: text/plain' https://google.com/login
text/plain
- send a post request with data from a file
curl -d '@data.txt' https://google.com/login
Content-Type=application/x-www-form-urlencoded
- url-encode post data
curl --data-urlencode 'comment=hello world' https://google.com/login
If your data is not URL-encoded, then replace -d with --data-urlencode. It works exactly the same way as -d, except the data gets URL-encoded by curl before it’s sent out on the wire.
- post a binary file
curl -F 'file=@photo.png' https://google.com/profile
Content-Type=multipart/form-data
- post a binary file and set its mime type
curl -F 'file=@photo.png;type=image/png' https://google.com/profile
If no type is specified, then curl sets it to application/octet-stream.
- post a binary file and change its filename
curl -F 'file=@photo.png;filename=me.png' https://google.com/profile
Similar to the previous two recipes, this recipe uses the -F argument to upload a photo.png via a POST request. Additionally, in this recipe, the filename that is sent to the web server is changed from photo.png to me.png. The web server only sees the filename me.png and doesn’t know the original filename was photo.png.
- construct two query arguments
curl -G -d 'q=kitties' -d 'count=20' https://google.com/search
- url-encode a query argument
curl -G --data-urlencode 'q=kitties' -d 'count=20' https://google.com/search
- change the user agent
curl -A 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0' https://google.com
curl -A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36' https://google.com
curl -A 'Googlebot/2.1 (+http://www.google.com/bot.html)' https://washingtonpost.com
curl -H 'User-Agent: php/1.0' https://google.com
- cookie
curl -b 'session=abcdef' https://google.com
curl -b 'session=abcdef' -b 'loggedin=true' https://google.com
curl -b 'session=' https://google.com
curl -c cookies.txt https://www.google.com
curl -b cookies.txt https://www.google.com
- add a referrer
curl -e 'https://google.com?q=cats' http://catonmat.net
curl -e '' http://catonmat.net
curl -H 'Referer: https://digg.com' http://catonmat.net
- follow any redirect(3xx)
curl -L http://catonmat.net
- use the basic http authentication
curl -u 'bob:12345' https://google.com/login
curl https://bob:12345@google.com/login
curl -u 'bob:' https://google.com/login
curl -u 'bob' https://google.com/login
- use a proxy
curl -x socks4://james:cats@myproxy.com:8080 https://catonmat.net
curl -x socks5://james:cats@myproxy.com:8080 https://catonmat.net
curl -x james:cats@myproxy.com:8080 https://catonmat.net
curl --no-proxy host.com -x https://myproxy.com:8080 host.com https://catonmat.net https://digg.com https://host.com
- save the response to file
curl -o response.txt https://google.com?q=kitties
curl -O https://catonmat.net/ftp/digg.pm
- debug
curl -i https://catonmat.net
curl -s -o /dev/null -D - https://catonmat.net
curl -v https://catonmat.net
curl --trace - https://catonmat.net
curl --trace - --trace-time https://catonmat.net
curl -s -o /dev/null -D - https://catonmat.net
curl -v -s -o /dev/null --stderr - https://catonmat.net | grep '^>'
curl -w '%{response_code}' -s -o /dev/null https://catonmat.net