Basic cURL Commands
You can use CURL to make HTTP requests. Here are some examples to get started.
Show the options:
curl --help
Fetch a url (a simple GET request):
curl www.yahoo.com
Show only the response headers of a request:
curl -I www.yahoo.com
Show 'verbose' info (this will include the request headers, among other things):
curl -v www.yahoo.com
Output the contents of the url to a text file:
curl -o yahoo.txt www.yahoo.com
The -I option will only show the headers that are sent back:
curl -v -I www.yahoo.com
Make a GET request:
curl --request GET www.yahoo.com
# or
curl --request GET 'www.yahoo.com'
Make a post request:
curl --request POST 'www.yahoo.com'
Make a post request and send data (submitting form data):
curl --request POST http://some-host/some-path/ --data 'firstName=Bob&lastName=Smith&email=bob@smith.com&phone=555-555-5555' -H 'Content-Type: application/x-www-form-urlencoded'
Make a post request and send JSON data (note the Content-Type header):
curl --request POST http://some-host/some-path/ --data '{"firstName":"Bob","lastName":"Smith",email":"bob@smith","phone":"555-555-5555"}' -H 'Content-Type: application/json' -H 'Accept: application/json'
To add a request header:
curl http://some-host/some-path/ --header 'someheader:somevalue'
To add multiple request headers:
curl http://some-host/some-path/ -H "Cookie: user=Bob; password=test" -H "Accept:application/json"
Here's an example that sends a cookie header:
curl -v -I http://some-host/some-path/ -H "Cookie: user=Bob; password=test"
A PUT request to the 'contacts' endpoint (note the '101' in the URL):
curl --request PUT http://some-host/contacts/101 --data '{"id":101, firstName":"Bob","lastName":"Smith",email":"bob@smith","phone":"555-555-5555"}' -H 'Content-Type: application/json' -H 'Accept: application/json'
A DELETE request to the 'contacts' endpoint (note the '101' in the URL):
curl --request DELETE http://some-host/contacts/1