用libeven写一个post请求
时间: 2024-05-01 20:18:04 浏览: 8
好的,下面是使用libcurl发送POST请求的代码示例:
```c++
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
struct curl_slist *headers = NULL;
const char *url = "http://example.com/api";
const char *data = "{\"key\":\"value\"}";
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "charset: UTF-8");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}
curl_easy_cleanup(curl);
curl_slist_free_all(headers);
}
return 0;
}
```
以上代码使用libcurl库发送一个POST请求。其中,url是请求的地址,data是POST请求的数据体,headers是请求头集合,这里设置了Content-Type和charset两个请求头。注意,在实际使用中,应该根据实际情况适当调整请求头。另外,由于数据体通常是JSON格式的,所以Content-Type应该设置为application/json。