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

时间:2014-03-20 19:55:24

标签: libcurl

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

1 个答案:

答案 0 :(得分:7)

我认为在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);

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

相关问题