Curl examples
Table of contents
curl is a CLI tool that allows you to transfer data to or from a server in a variety of different protocols. In this post, you can find some examples that demonstrate the basic curl options for the HTTP/HTTPS protocols.
Make a GET request
GET is the default HTTP method, so you don’t have to use an option. You can pass only the URL:
curl https://www.google.comSpecify the HTTP Method
You can specify the HTTP method with the -X or --request option. In the following example, I make an OPTIONS request:
curl -X OPTIONS -i https://jsonplaceholder.typicode.com/The -i or --include option gives back the response headers, not only the response body—more about that in a bit.
1. Perform a POST request
When you perform a POST request, you usually send some data to the server. You can send data with the -d or --data option:
curl https://jsonplaceholder.typicode.com/todos \
--data "title=Groceries&body=Buy groceries."
# The backslash (\) spans the command over multiple linesYou don’t have to state the method with -X POST because POST is the default method when you use the --data option. You send the data as content-type: application/x-www-form-urlencoded. See also -F or --form and --data-raw.
2. Update with the PUT method
If you want to perform a PUT request, you have to specify the method along with the data you want to send:
curl https://jsonplaceholder.typicode.com/todos/1 \
-X PUT \
--data "title=Groceries&body=Buy groceries."Get the headers
When you make an HTTP request with curl, the default output you get is the response body. If you want to get the headers, you have 3 options:
1. Get both response headers and body
This is what you want most of the times. To get both response headers and body, you can use the -i or --include option:
curl https://www.google.com/ -i2. Get only the response headers
To get only the response headers—without the response body—use the -I or --head option:
curl https://www.google.com/ -IWith this option, you perform a HEAD HTTP request. You can achieve the same thing if you set the HTTP method to HEAD with curl -X HEAD. But because you get only the response body by default, you also have to include the headers with -i or --include, so the final command would be: curl -X HEAD -i. As a result, the -I or --head option is preferred.
3. Get the request headers too
Sometimes, you want more details about the exchange between the server and the client. For example, you may want to see the request headers. In this case, you can use the -v or --verbose option. You get a lot more information, though, not only the request headers:
curl https://www.google.com/ --verboseTo find the request headers, look for the lines that start with >. The response headers, on the other hand, start with <. The lines that start with an asterisk (*) are TCP or TLS stuff.
Follow redirects
To follow the redirects use the -L or --location option. You should also append the response headers with the -i option to see what’s going on:
curl http://google.com --location -iTry the same request without the --location option if you want to see the difference.
Save console output to file
You can use the -o or --output option to save the console output in a file:
curl https://www.google.com -i -o google.txtSave a file
If you want to save a file and keep the original filename in your local machine, use the -O or --remote-name option:
curl -O https://markoskon.com/static/fa71d793057a033cf6b13329754cb6fc/dbbca/all-cool-dev.jpgIf you want to give the file a new name, use the -o or --output instead:
curl -o gatsby-modals.jpg https://markoskon.com/static/fa71d793057a033cf6b13329754cb6fc/dbbca/all-cool-dev.jpgAdvanced
More useful options for another post:
- Upload a file with the
-T or --upload-fileoption. - Save multiple files with
-o or --output - Other options you have to send data to the server.
- Set headers with
-H or --headeroption.
Further reading
Other things to read
Popular
- Reveal animations on scroll with react-spring
- Gatsby background image example
- Extremely fast loading with Gatsby and self-hosted fonts