Запросы к REST API на Си
| Введение | |
| GET | |
| POST | |
| PUT | |
| DELETE | |
| Другие статьи о С++ |
Введение
Эта статья является частью большой шпаргалки по составлению REST запросов на разных языках программирования.
GET
CURL *hnd = curl_easy_init(); curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "GET"); curl_easy_setopt(hnd, CURLOPT_URL, "https://example.com/l-manager/api/v1/status"); struct curl_slist *headers = NULL; headers = curl_slist_append(headers, "accept: application/json"); curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers); CURLcode ret = curl_easy_perform(hnd);
POST
CURL *hnd = curl_easy_init(); curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST"); curl_easy_setopt(hnd, CURLOPT_URL, "https://example.com/r-store/api/v1/rs"); struct curl_slist *headers = NULL; headers = curl_slist_append(headers, "accept: application/json"); headers = curl_slist_append(headers, "Authorization: Bearer a-proper-token-goes-here"); headers = curl_slist_append(headers, "content-type: application/json"); curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, "{\"source_rules\":{\"type\":\"RULE\"},\"name\":\"name\",\"comment\":\"comment\",\"permit_agent\":true,\"access_group_id\":\"ag_id\"}"); CURLcode ret = curl_easy_perform(hnd);
PUT
CURL *hnd = curl_easy_init(); curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "PUT"); curl_easy_setopt(hnd, CURLOPT_URL, "https://example.com/r-store/api/v1/rs/r_id"); struct curl_slist *headers = NULL; headers = curl_slist_append(headers, "accept: application/json"); headers = curl_slist_append(headers, "Authorization: Bearer a-proper-token-goes-here"); headers = curl_slist_append(headers, "content-type: application/json"); curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, "{\"name\":\"r_name\",\"permit_agent\":true}"); CURLcode ret = curl_easy_perform(hnd);
DELETE
CURL *hnd = curl_easy_init(); curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "DELETE"); curl_easy_setopt(hnd, CURLOPT_URL, "https://example.com/r-store/api/v1/rs/r_id"); struct curl_slist *headers = NULL; headers = curl_slist_append(headers, "accept: application/json"); headers = curl_slist_append(headers, "Authorization: Bearer a-proper-token-goes-here"); curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers); CURLcode ret = curl_easy_perform(hnd);
Автор статьи: Андрей Олегович
РЕКЛАМА от Яндекса. Может быть недоступна в вашем регионе
Конец рекламы. Если там пусто считайте это рекламой моей телеги