Curl
The curl project provides two tools, the libcurl library and the curl command line tool.
The first is a client-side URL transfer library that supports a whole host of protocols.
The curl tool can be used for getting and sending data with a URL syntax and uses libcurl under the hood.
When you install curl, you get both tools and can use the curl command to perform requests and read the results.
We will use curl in some places to perform HTTP(s) calls, mostly to perform REST requests.
You will be provided with the basic commands you can copy and paste to use curl.
|
GUI Alternatives
If you prefer a GUI tool over a command line tool, Postman is one of the best-known ones. You can also install an extension to most browsers that will offer similar functionality - these are normally called REST Client or something similar. Yet Another REST Client is an example of an extension for Chromium based browsers. Most IDEs also have some functionality to perform requests and inspect the results. For VSCode, the REST Client extension is a good option. |
|
Formatting JSON
Many of the requests we’ll perform return JSON by default.
Reading JSON in plain text is a bit cumbersome, so there are additional tools you can use to improve the experience, if you want to.
One of those tools is jq.
Instead of the plain text output, the result is now coloured (and formatted, although this particular service already does that as a convenience), making it easier to read.
Figure 1. Example of JSON output formatted with
jqIf you want to, you can additionally install |
Linux
The curl program is pre-installed on most distributions.
Test whether you can use curl by going to your terminal application and run the following command.
$ curl -V
Note that the -V flag is a capital letter V.
You should see the version information of the curl program being printed.
curl -Vcurl 8.2.1 (x86_64-pc-linux-gnu) libcurl/8.2.1 OpenSSL/3.1.2 zlib/1.2.13 brotli/1.0.9 zstd/1.5.5 libidn2/2.3.4 libpsl/0.21.2 (+libidn2/2.3.4) libssh2/1.11.0 nghttp2/1.55.1
Release-Date: 2023-07-26
Protocols: dict file ftp ftps gopher gophers http https imap imaps mqtt pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp
Features: alt-svc AsynchDNS brotli GSS-API HSTS HTTP2 HTTPS-proxy IDN IPv6 Kerberos Largefile libz NTLM NTLM_WB PSL SPNEGO SSL threadsafe TLS-SRP UnixSockets zstd
If you get a message that the curl program cannot be found, install it first from the website or use your distribution’s package to install it from the repositories.
|
Check that you can access a URL with another curl command.
$ curl -I http://www.example.com
The result will be the HTTP headers returned by the website, if the command is successful.
curl -IHTTP/2 200
content-encoding: gzip
accept-ranges: bytes
age: 577595
cache-control: max-age=604800
content-type: text/html; charset=UTF-8
date: Wed, 23 Aug 2023 06:55:00 GMT
etag: "3147526947+gzip"
expires: Wed, 30 Aug 2023 06:55:00 GMT
last-modified: Thu, 17 Oct 2019 07:18:26 GMT
server: ECS (dcb/7EA2)
x-cache: HIT
content-length: 648
That’s it for curl!