Introduction
curl (Client for URLs") - is a command-line tool (Windows Git Bash or Unix/Linux) for interacting with servers to download or send data.
Curl supports protocols such as: HTTP, HTTPS, FTP, FTPS, SCP, SFTP, SMB, LDAP, TFTP, RTMP, MQTT, IMAP, POP3, GOPHER, DICT, FILE, LDAP, LDAPS, RTSP, Telnet.
Example
You can test the examples using any publicly available API. On the Internet you will find many websites such as website providing information about countries.
GET
The default action when calling curl is to perform an HTTP GET request (data retrieval operation), so an optional, equivalent parameter is "-X GET":
curl -X GET https://restcountries.com/v3.1/capital/tallinn
The website above returns information based on the capital of a given country. If the information returned by the server is in JSON format, it can be additionally formatted to improve readability using the jq command (if available).
POST
To send data to the server (to create a new entry), you can execute the POST command:
curl -X POST -d "param1=value1¶m2=value2" http://example.com/resource
If the data is in the form of JSON, it is worth remembering to add the Content-Type HTTP header using the -H flag:
curl -X POST -H "Content-Type: application/json" -d '{"key1":"value1","key2":"value2"}' http://example.com/resource
If the data is in the data.json file, you can include it as follows:
curl -X POST -H "Content-Type: application/json" -d @data.json http://example.com/resource
The data.json file should look similar to the following:
{
"key1": "value1",
"key2": "value2"
}
DELETE
To delete data, perform the DELETE operation::
curl -X DELETE http://example.com/resource/123
PUT
To update already existing data, perform the PUT operation:
curl -X PUT -d "param1=newValue1¶m2=newValue2" http://example.com/resource/123
If the data to be updated is in an external file, proceed in the same way as in the POST command.
CERTIFICATE
If you receive server certificate verification errors when trying to connect to the server, you can ignore them using the -k flag (see "other flags") or add your own CA certificate store (truststore) so that the client (i.e. curl) can verify the server certificate. Similarly, we can add a client certificate if the server requires the client to authenticate with a certificate during connection:
curl --cert-type P12 --cert client.pfx:password --cacert /path/to/truststore.pem https://example.com/resource
The above example uses a PKCS#12 certificate standard, but the "curl" command also supports others.
FTP
To send the localfile.txt file to the FTP server, execute the command:
curl -T localfile.txt ftp://ftp.example.com/upload/
OTHER FLAGS
Saving the request result to a file:
curl -o output.txt http://example.com/file.txt
HTTP authorization using login and password:
curl -u username:password http://example.com/resource
Adding the cookie.txt file to the HTTP request:
curl -b cookies.txt http://example.com
Ignoring SSL/TLS certificate errors (not recommended in production):
curl -k https://example.com
Sending a file to the server as part of a PUT request:
curl --upload-file localfile.txt http://example.com/upload
Summary
The "curl" command is widely used due to its simplicity. It enables the automation of repetitive activities through use in scripts. To get all available options you can run the command:
curl -h
You can get a more detailed list by doing:
curl --help all
It is worth remembering that in older versions of the "curl" program some of the options will not be available. In this case, it is worth checking the currently available version:
curl --version
and compare to the information contained on the website: https://curl.se/docs/releases.html
Good luck!