如何在 c++ 中使用 libcurl 将 POST 发送到 elasticsearch
How to use libcurl in c++ to send POST to elasticsearch
>有人知道将 Elasticsearch 与 libcurl 一起使用的确切语法吗?
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:9200/bro-201409170900/http/ZinAvJ-ETT-mycy2jyRkdg/_update -d");
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "{"script" : "ctx._source.longitude += 3"}");
curl_easy_perform(curl);
curl_easy_cleanup(curl);
return 1;
}
这段代码不会更新经度参数,我不知道为什么。
不应在 url 中指定 "-d"。命令行工具只是建立在libcurl之上。如果你想看看 post 请求的 c 代码是什么样子,你可以使用 libcurl 选项和命令行。
例:
curl localhost:9200/bro-201409170900/http/ZinAvJ-ETT-mycy2jyRkdg/… -d '{ "script" : "ctx._source.longitude += 2"}' --libcurl output.c
一个简单的"C"实现看起来就像这些行一样
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
char *postFields = "{"script" : "ctx._source.longitude += 3"}";
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:9200/bro-201409170900/http/ZinAvJ-ETT-mycy2jyRkdg/_update");
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl,CURLOPT_POSTFIELDS,postFields);
curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,strlen(postFields));
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
return res;
}
相关文章:
- 如何使用curlpp通过POST方法上传文件和json数据
- 使用libcurl提交批量url的正确BING Api POST url是什么
- 命名空间 pqxx 无法识别 - 将 libpqxx 添加到 Windows 上的 Visual Studio Post
- 如何使用boost/beast从HTTP POST请求中解析和提取有效负载?
- 如何使用 curl 将 POST 请求从 python 重写为 C++
- 从简单的 POST 请求中解析异常
- Visual Studio 2017 中的 cpprestapi 问题找不到 POST/GET 常量
- 使用 cpp-httplib 的 POST 请求
- POST Arduino 传感器值通过 Flask API 发送到本地 Sqlite db
- C++:如何通过 curl 调用使用 HTTP post 请求发送二进制数据(protobuf 数据)
- C++中的 cURL POST 请求
- C++ Schannel POST 400 错误代码错误请求
- c++curl返回413请求实体过大,但是post大小远小于max大小
- HTTP POST 请求不允许我定义上下文类型
- 如何在c++中使用libcurl发送POST请求并接收它
- cpp libcurl 在 HTTP post 调用中发送 zip 文件,而不使用 multipart\form-da
- 使用 http post 在 FCM 中推送通知
- 我想将 POST 请求从 arduino 发送到 mysql 工作台并保存值
- 如何捕获从 MFC Web 浏览器 c++ 发出的 GET/POST 请求
- 如何在 c++ 中使用 libcurl 将 POST 发送到 elasticsearch