Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
json格式的libcurl http get请求_C_Http_Curl_Get_Libcurl - Fatal编程技术网

json格式的libcurl http get请求

json格式的libcurl http get请求,c,http,curl,get,libcurl,C,Http,Curl,Get,Libcurl,有没有一种方法可以使用JSON格式的libcurl发送HTTP get请求 我目前的要求是 curl\u easy\u setopt(curl\u句柄,CURLOPT\u URL,”http://localhost:9200/_search?q=tag:warcraft“” 使用libcurl。它在旋度上是等价的 curl -XGET http://localhost:9200/_all/tweet/_search?q=tag:warcraft 我想使用libcurl发送以下curl请求(js

有没有一种方法可以使用JSON格式的libcurl发送HTTP get请求

我目前的要求是

curl\u easy\u setopt(curl\u句柄,CURLOPT\u URL,”http://localhost:9200/_search?q=tag:warcraft“”

使用libcurl。它在旋度上是等价的

curl -XGET http://localhost:9200/_all/tweet/_search?q=tag:warcraft
我想使用libcurl发送以下curl请求(json格式)

curl -XGET http://localhost:9200/_search -d '{
    "query" : {
        "term" : { "tag": "warcraft" }
    }
}'

我想知道发送上述请求的等效libcurl代码。谢谢。

您应该使用CURLOPT_POSTFIELDS

curl_easy_setopt(curl, CURLOPT_POST, 1); 
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_data_encoded_as_string);
-d选项用于POST方法。从curl手册页

-d、 --数据将POST请求中的指定数据发送到 HTTP服务器

如果需要发送更多无法放入查询字符串的数据,则必须使用POST方法

作为GET请求的一部分,可以在URI的 查询字符串,例如指定搜索词、日期范围或 定义查询的其他信息。作为POST请求的一部分, 任何类型的任意数量的数据都可以以相同的方式发送到服务器 请求消息正文

如果您必须严格使用GET(?)来形成您的url,以便将json数据放入查询字符串本身

query_string = "q=" + json_encoded_to_str
curl_easy_setopt(curl_handle, CURLOPT_URL, "http://localhost:9200/_search?" + query_string)

按照kalyan的建议,这就是我最终得到的代码。张贴此完成

int main() {

     CURL *curl_handle;     
     CURLcode res;

    static const char *postthis="{\"query\":{\"term\":{\tag\":\"warcraft\"}}}";
    curl_global_init(CURL_GLOBAL_ALL);
    curl_handle = curl_easy_init();

    if(curl_handle) {

        curl_easy_setopt(curl_handle, CURLOPT_URL, "http://localhost:9200/_search");
        curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, postthis);

        curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDSIZE, (long)strlen(postthis));
        curl_easy_setopt(curl_handle, CURLOPT_WRITEHEADER, stdout);
        curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, stdout);

        res = curl_easy_perform(curl_handle);
        if(res != CURLE_OK)
                  fprintf(stderr, "curl_easy_perform() failed: %s\n",
                                        curl_easy_strerror(res));
        curl_easy_cleanup(curl_handle);
        return 0;
    }
}

我认为CURLOPT_POST和CURLOPT_POST字段与http POST请求相关。我需要一些用于HTTP GET request.IMO的东西,这不是一个好主意。但仍然可以实现。答案已更新。谢谢。我采纳了你关于使用POST方法的建议,并使它发挥了作用。我遵循了这个代码框架@迪内斯里拉姆不错!快乐编码。