Libcurl请求没有结果

Libcurl request no result

本文关键字:结果 请求 Libcurl      更新时间:2023-10-16

我在c++中使用libcurl扩展。我发送一个请求到elasticsearch服务器,但结果是' null '。我在PHP中重建了curl请求,它工作了。我怎样才能从web服务器得到结果?

CURL *curl;
CURLcode res;
std::stringstream ss_url; 
ss_url << "http://IP/newgame/player/2/_update";
char *post_url = const_cast<char*>(ss_url.str().c_str());

curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, post_url);
curl_easy_setopt(curl, CURLOPT_PORT, 9200);
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl,CURLOPT_POSTFIELDS, '{ "doc": { "x": 555555, "y": 287273 }}');
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
编辑:

我已经尝试记录curl的答案。但是文件是空的

FILE *curllog = fopen("/home/elasticsearch_curl.log", "w+");
curl_easy_setopt(curl, CURLOPT_WRITEDATA, curllog);

检查post_url强制转换是否有问题,将其替换为实际字符串。in curl_set_opt of CURLOPT_URL

curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com");

这可能是问题所在。这对我很有效。包括在curl_easy_perform之后进行调试和检查。

if(res != CURLE_OK) { fprintf(stderr, "curl_easy_perform()failed:%sn",curl_easy_strerror(res));}

一个有效的GET示例:https://gist.github.com/k3ut0i/737b6d19713ef5aa0a74