curl

API testing image from www.aredel.com
Photo: freepik.com

Introduction

In this article you can learn how to make requests to a REST API using the popular curl utility.

You can make requests to my second site - answerit.ru , which was made specifically for training QA engineers. You can view the API documentation here

Ask a question on Telegram @aofeedchat

Support the project

Installing curl

First you need to check whether curl is installed

curl -V

Command 'curl' not found, but can be installed with:

sudo apt install curl

If it is not installed, run

sudo apt install -y curl

And if you have CentOS , Rocky , RedHat or another .rpm-based distro, then

sudo yum install -y curl

[sudo] password for andrei: Reading package lists... Done Building dependency tree Reading state information... Done The following NEW packages will be installed: curl 0 upgraded, 1 newly installed, 0 to remove and 6 not upgraded. Need to get 161 kB of archives. After this operation, 411 kB of additional disk space will be used. Get:1 http://fi.archive.ubuntu.com/ubuntu focal-updates/main amd64 curl amd64 7.68.0-1ubuntu2.4 [161 kB] Fetched 161 kB in 0s (2,485 kB/s) Selecting previously unselected package curl. (Reading database ... 302218 files and directories currently installed.) Preparing to unpack .../curl_7.68.0-1ubuntu2.4_amd64.deb ... Unpacking curl (7.68.0-1ubuntu2.4) ... Setting up curl (7.68.0-1ubuntu2.4) ... Processing triggers for man-db (2.9.1-1) ...

After installation you need to confirm success

curl -V

curl 7.68.0 (x86_64-pc-linux-gnu) libcurl/7.68.0 OpenSSL/1.1.1f zlib/1.2.11 brotli/1.0.7 libidn2/2.2.0 libpsl/0.21.0 (+libidn2/2.2.0) libssh/0.9.3/openssl/zlib nghttp2/1.40.0 librtmp/2.3 Release-Date: 2020-01-08 Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp scp sftp smb smbs smtp smtps telnet tftp Features: AsynchDNS brotli GSS-API HTTP2 HTTPS-proxy IDN IPv6 Kerberos Largefile libz NTLM NTLM_WB PSL SPNEGO SSL TLS-SRP UnixSockets

GET

curl -X GET https://www.answerit.ru/flaskapi/api/get

200

With a header

curl --request GET \ --url https://example.com/engine/api/v1/wfs \ --header 'Authorization: Bearer a-proper-token-goes-here' \ --header 'accept: application/json'

PUT

curl -X PUT https://www.answerit.ru/flaskapi/api/put

200

PUT with a number

curl -X PUT https://www.answerit.ru/flaskapi/api/order/2020

Order with id 2020 was added to the system. Thank you!

curl --request PUT \ --url https://example.com/engine/api/v1/wfs/wf_id \ --header 'Authorization: Bearer a-proper-token-goes-here' \ --header 'accept: application/json' \ --header 'content-type: application/json' \ --data ' { "status": "WAITING", "approver_can_revoke": true, "target_role_revoked": false, "can_bypass_revoke_wf": false, "name": "name" } '

POST

curl -X POST https://www.answerit.ru/flaskapi/api/post/1961

Post 1961

curl --request POST \ --url https://example.com/engine/api/v1/wfs \ --header 'Authorization: Bearer a-proper-token-goes-here' \ --header 'accept: application/json' \ --header 'content-type: application/json' \ --data ' { "status": "WAITING", "approver_can_revoke": true, "target_role_revoked": false, "can_bypass_revoke_wf": false, "steps": [ { "name": "step", "match": "ANY" } ], "grant_type": "PERMANENT", "name": "name" } '

DELETE

curl --request DELETE \ --url https://example.com/engine/api/v1/wfs/wf_id \ --header 'Authorization: Bearer a-proper-token-goes-here' \ --header 'accept: application/json'

Article author: Andrei Olegovich