C++ HTTP post使用libcurl创建XDB数据库
我正在尝试使用libcurl库制作一篇http文章,以创建一个XDB数据库,如他们的网站所示: curl-i-XPOSThttp://localhost:8086/query -数据urlencode q=创建数据库mydb 看来我的代码不起作用了。它没有给我任何错误,但数据库没有创建。但是,如果我尝试使用相同的函数向现有数据库中添加一些点,它就会起作用。我想我错过了添加q=createdatabasemydb部分的正确方法。我应该如何更改代码C++ HTTP post使用libcurl创建XDB数据库,c++,http-post,libcurl,influxdb,C++,Http Post,Libcurl,Influxdb,我正在尝试使用libcurl库制作一篇http文章,以创建一个XDB数据库,如他们的网站所示: curl-i-XPOSThttp://localhost:8086/query -数据urlencode q=创建数据库mydb 看来我的代码不起作用了。它没有给我任何错误,但数据库没有创建。但是,如果我尝试使用相同的函数向现有数据库中添加一些点,它就会起作用。我想我错过了添加q=createdatabasemydb部分的正确方法。我应该如何更改代码 int main(int argc, char *
int main(int argc, char *argv[]){
char *url = "http://localhost:8086/query";
char *data = "q=CREATE DATABASE mydb";
/* should i change data string to json?
data = "{\"q\":\"CREATE DATABASE mydb\" }" */
bool res = createInfluxDB(url, data);
/*control result*/
return(0);
}
bool createInfluxDB(char *url, char *data) {
CURL *curl;
curl = curl_easy_init();
if(curl) {
CURLcode res;
/* What Content-type should i use?*/
struct curl_slist* headers = curl_slist_append(headers, "Content-Type: application/json");
/*--data-urlencode*/
char *urlencoded = curl_easy_escape(curl, data, int(strlen(data)));
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, urlencoded);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(urlencoded));
res = curl_easy_perform(curl);
/*omitted controls*/
curl_free(urlencoded);
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
return(true);
}
编辑答案:
在if语句中仍然有一个命名错误的变量:
if (urlencode) free(...
那应该是
if (urlencoded) free(...
然后在您的头文件中将应用程序类型设置为json,我认为这不是您想要的。
类似application/x-www-form-urlencoded的东西可能更好
struct curl_slist* headers = curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded");
原始答案:
解决方法可能在您的
char *data = curl_easy_escape(curl, json, int(strlen(json)));
用不存在的json变量重载数据
类似的方法可能会更好:
data = curl_easy_escape(curl, data, int(strlen(data)));
在分析了http post请求返回错误请求的数据包之后,我得出结论,我不应该将查询参数添加为数据。但它应该是url的一部分。所以,在像这样更改代码之后,它就可以工作了
int main(int argc, char *argv[]){
char *url = "http://localhost:8086/query?q=CREATE+DATABASE+mydb";
bool res = createInfluxDB(url);
/*control result*/
return(0);
}
bool createInfluxDB(char *url) {
CURL *curl;
curl = curl_easy_init();
if(curl) {
CURLcode res;
struct curl_slist* headers = curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
res = curl_easy_perform(curl);
/*omitted controls*/
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
return(true);
}
你检查过你的数据库用户权限了吗?也许您的呼叫是正常的,但DB阻止了该操作。@Yann,但它会拒绝连接并返回false,不是吗?正如我所说,我可以用同样的方法将数据添加到现有数据库中。嘿@user mydb数据库不存在吗?@Yann不,我也尝试了其他名称,但不起作用。谢谢,这是我在这里复制代码时犯的错误。。。无论如何,在您编码时我再次检查了一遍,但仍然不起作用。@user3717434我用您所做的代码更改编辑了我的答案。我认为您使用application/x-www-form-urlencoded是正确的,但仍然不起作用。我还试图将数据更改为q=CREATE+DATABASE+mydb,但没有办法。好的,wp:考虑接受你的答案以结束问题。