如何使用libcurl发送HTTP删除请求

Jit*_*dra 5 libcurl

我需要使用"Content-Type:application/json"进行HTTP DELETE调用.我怎么能用libcurl接口做到这一点.

Fer*_*ndo 9

我认为在C++中使用它的正确方法是这样的:

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);
Run Code Online (Sandbox Code Playgroud)

注意:我已经编辑了答案,包括John建议的清理代码.