curl

Содержание
Введение
Установка curl
GET
PUT
PUT: передать число
POST
DELETE
Связанные статьи
Тестирование API изображение с сайта www.andreyolegovich.ru
Фото: freepik.com

Введение

В этой статье вы можете узнать, как выполнить запросы к REST API, с помощью популярной утилиты curl.

Запросы можно делать к моему второму сайту - answerit.ru , который сделан специально для обучения QA-инженеров. Посмотреть документацию к API можно здесь

Задать вопрос в телеграм @aofeedchat

Поддержать проект

Установка curl

Сперва нужно проверить установлен ли curl

curl -V

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

sudo apt install curl

Если не установлен - нужно выполнить

sudo apt install -y curl

А если у вас CentOS , Rocky , RedHat или другой .rpm то

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) ...

После установки нужно убедиться в успехе

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

С 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 с числом

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'