curl: the basics
Learn how to use curl to download files, send GET and POST requests, and more.
Table of Contents
- Download files
- Get file content right in the terminal (valid for text files)
- Get response headers from a request
- Send user and password with “Basic Authentication” requests
- Specify a HTTP method
- Add headers to a request
- Add a referer header to a request
- Add POST data to a request
Download files
curl -O <url>
$ curl -O https://rs1.es/assets/imgs/evince-1.jpg
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 106k 100 106k 0 0 218k 0 --:--:-- --:--:-- --:--:-- 218k
If the website is something like this: http://example.com
, you need to specify the index.html
page: http://example.com/index.html
. This is only necessary when using -O
.
Get file content right in the terminal (valid for text files)
# Same as download but without -O
$ curl https://rs1.es/robots.txt
Sitemap: https://rs1.es/sitemap.xml
Get response headers from a request
curl -I <url>
$ curl -I https://rs1.es/
HTTP/2 200
date: Thu, 23 Sep 2021 16:42:58 GMT
content-type: text/html
...
Send user and password with “Basic Authentication” requests
curl -u <user>:<password> -O <url>
Specify a HTTP method
curl -X <METHOD> <url>
# GET, POST, DELETE,...
Add headers to a request
curl -X <METHOD> -H "HeaderName: headerValue" <url>
curl -X POST \
-H "Authorization: Bearer 12345" \
-H "Content-Type: application/json" \
https://api.example.com/stuff
Add a referer header to a request
curl --referer https://example.com -O <url>
Add POST data to a request
curl -X POST -d '<probably JSON data>' <url>
curl -X POST \
-H "Content-Type: application/json" \
-d '{"name":"ricardo"}' \
https://api.example.com/stuff
If you have any suggestion, feel free to contact me via social media or email.
Latest tutorials and articles:
Featured content: