curl tutorial with examples of usage

Curl is a linux utility that is used to make HTTP requests to a given url. It outputs HTTP response to standard output and is actually very easy to use. Here are some examples to show its usage:

Make a GET request without any data:
curl --request GET ''

Both usages are actually the same. However, I try to use second one all the time. With --request (or -X) parameter, we choose our http method to use for our requests. Its values can be GET, POST, DELETE, PUT etc.. If we don't specify this parameter, GET method will be used by default. That is why first version works same as the second one.

Make requests with different HTTP method type without data:
 curl --request POST ''
 curl --request DELETE ''
 curl --request PUT ''
Make requests with data:

Since we learn how to make POST, GET, PUT, DELETE requests, we can now make same requests with data. In order to send data with those requests, we should use --data parameter. Here are some examples:

# send login data with POST request
curl --request POST '' \
--data 'username=myusername&password=mypassword'

# send search data to with get request
curl --request GET ''

# send PUT request with data
curl --request PUT ''\
--data ''

# same thing but this one get data from a file named data.txt
curl --request PUT ''\
--data @data.txt
Make requests with extra headers

Sometimes you need to add HTTP headers to your requests. This is done by --header parameter.

curl --request GET '' \
--header 'sessionid:1234567890987654321'

Notice that we are using colon(":") to separate header name from its value.

Get response with HTTP headers

If you need to get HTTP headers with your response, --include parameter can be used

 curl --request GET '' --include

Those examples cover most of the stuff curl is needed for. If you need more functionality, you can use following two commands as a reference

# gives  brief description of parameters
curl --help

# curl manual page
man curl

TODO: add cookie parameters


blog comments powered by Disqus