Mastering curl on Linux: Download Files and Consume APIs
The curl command on Linux can be used to download files, as well as communicate with Web APIs directly from the Linux shell. It is a very versatile command for programmers to test access to APIs of software and applications hosted on the web.
The basic syntax of curl is:
$ curl URL
Example:
$ curl https://google.com<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8"><TITLE>301 Moved</TITLE></HEAD><BODY><H1>301 Moved</H1>The document has moved<A HREF="https://www.google.com/">here</A>.</BODY></HTML>
This way, it sends a GET request to the specified server, on the specified page. You can pass GET parameters directly in the URL using the separators "?" and "&".
$ curl http://api.servidor.com/myapi/2.5/tempo?id=217
When passing more than one parameter in the URL, especially when using the symbol "&", you must use single quotes around the URL:
$ curl 'http://api.servidor.com/myapi/2.5/tempo?id=217&user=uira'
Curl allows you to specify a timeout in seconds for which it will wait for a server response, using the "-m" option:
$ curl -m 3 'http://api.servidor.com/myapi/2.5/tempo?id=2172797&user=uira'
You can make a POST request using curl, with the "-X" option:
$ curl -X POST 'http://api.servidor.com/myapi/2.5/tempo'
The -X option allows the methods GET (default), POST and PUT.
To send data in the POST request, you must use the "-d" option. The data must be in URL encoded format.
$ curl -d 'id=217&user=uira' -X POST 'http://api.servidor.com/myapi/2.5/tempo'
You can also separate the data into multiple "-d" options:
$ curl -d id=217 -d user=uira -X POST 'http://api.servidor.com/myapi/2.5/tempo'
You can also specify a file containing the data to send via POST, using the "-d" option with "@":
$ curl -d @arquivo -X POST 'http://api.servidor.com/myapi/2.5/tempo'
If the site requires simple HTTP authentication, the "-u" option can send a username and password:
$ curl -u uira@senha -d id=217 -d user=uira -X POST 'http://api.servidor.com/myapi/2.5/tempo'
To send a JSON file, you need to specify the HTTP header, using the "-H" option:
$ curl -H "Accept: application/json" -d @arquivojson -X PUT 'http://api.servidor.com/myapi/2.5/tempo'
The "-i" option allows you to see the HTTP response from the server:
$ curl -i 'https://api.openweathermap.org/data/2.5/weather?q=Belo%20Horizonte,PT&appid=9908ae7bbb3c530f54efdec77ac3ccde'
HTTP/1.1 200 OK
Server: openresty
Date: Tue, 12 May 2020 22:13:25 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 473
Connection: keep-alive
X-Cache-Key: /data/2.5/weather?q=belo%20horizonte,pt
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET, POST
{"coord":{"lon":-7.42,"lat":39.31},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02n"}],"base":"stations","main":{"temp":286.83,"feels_like":285.15,"temp_min":286.48,"temp_max":288.15,"pressure":1008,"humidity":87},"visibility":10000,"wind":{"speed":3.1,"deg":220},"clouds":{"all":20},"dt":1589321586,"sys":{"type":1,"id":6402,"country":"PT","sunrise":1589260722,"sunset":1589312008},"timezone":3600,"id":2270968,"name":"Belo Horizonte","cod":200}
Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.
Liked it? Share
That was just one piece. The course teaches the whole path — from your first command to exam day.