Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send HTTP delete request using libcurl

Tags:

libcurl

I need to make a HTTP DELETE call with "Content-Type: application/json". How can i do this using libcurl interface.

like image 717
Jitendra Avatar asked Mar 20 '14 19:03

Jitendra


1 Answers

I think that the proper way to do it in C++ would be something like this:

CURL *hnd = curl_easy_init();
curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_easy_setopt(hnd, CURLOPT_URL, "http://some/url/");
curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, "{\"key\": \"value\"}");
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "content-type: application/json");
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);
CURLcode ret = curl_easy_perform(hnd);
// do something...
curl_slist_free_all(headers);
curl_easy_cleanup(hnd);

Note: I've edited the answer to include the cleanup code as suggested by John.

like image 168
Fernando Avatar answered Nov 08 '22 16:11

Fernando