Skip to content

Curl command on Linux (download files)

The curl command on Linux can be used to download files, as well as talk to Web APIs directly from the Linux shell. It is a very versatile command for programmers to test access to the APIs of software and applications hosted on the web.

The basic syntax of curl is:

$curl URL

Example:

$ curl https://google.com <br></br><title>301 Moved</title> <br></br><br></br><h1>301 Moved</h1> <br></br>The document has moved <br></br><A HREF=” https://www.google.com/">here.

In this way, it sends a GET request to the specified server on the provided page. Is it possible to pass GET parameters directly in the URL using the “tabs**?** “and” &”.

$ curl http://api.servidor.com/myapi/2.5/tempo?id=217

It is necessary when passing more than one parameter in the URL, especially using the ”& “symbol, to use single quotes in the URL:

$curl 'http://api.servidor.com/myapi/2.5/tempo?id=217&user=uira'

Curl allows you to enter a timeout in seconds for which it will wait for a response from the server, with the option “-m “:

$ curl -m 3 'http://api.servidor.com/myapi/2.5/tempo?id=2172797&user=uira'

It is possible to make a POST call using curl, with the “-X” option:

$curl -X POST 'http://api.servidor.com/myapi/2.5/tempo'

The -X option allows the GET (default), POST, and PUT methods.

To send data in the POST request, the “-d” option must be used. The data must be in the 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'

It is also possible to specify a file containing the data for sending via POST, using the option “-d” 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, it is necessary to specify the HTTP header, with the option “-H “:

$ 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_like” :285.15, "temp_like” :285.15, "temp_like” 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”: EN”, "sunrise” :1589260722, "sunset” :1589312008}, "timezone” :3600, "id” :2270968, "name”: Belo Horizonte”, "cod” :200}

Learn much more about Linux in our online course. You can register here. If you already have an account, or want to create one, just log in or create your user here.

Did you like it?

Share