libcurl http获取请求以json格式

编程入门 行业动态 更新时间:2024-10-23 12:29:10
本文介绍了libcurl http获取请求以json格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有没有办法使用libcurl以JSON格式发送HTTP get请求?

Is there a way to send a HTTP get request using libcurl in JSON format?

我当前的请求是

curl_easy_setopt(curl_handle,CURLOPT_URL,http:// localhost:9200 / _search?q = tag:warcraft)

使用libcurl。它等价于curl是

using libcurl. It's equivalent in curl is

curl -XGET localhost:9200/_all/tweet/_search?q=tag:warcraft

我想使用libcurl发送以下curl请求>

I would like to send the following curl request (in json format) using libcurl.

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

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

I would like to know the equivalent libcurl code to send the above request. Thanks.

推荐答案

您应该使用CURLOPT_POSTFIELDS

you should use CURLOPT_POSTFIELDS

curl_easy_setopt(curl, CURLOPT_POST, 1); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_data_encoded_as_string);

-d选项用于POST方法。从curl手册页

The -d option is used for POST method. From the curl man page

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

-d, --data Sends the specified data in a POST request to the HTTP server

如果您需要发送更多不适合查询字符串的数据,您必须使用POST方法

If you need to send more data that can not fit in a query string you have to use POST method

en.wikipedia/wiki/POST_ (HTTP)

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

As part of a GET request, some data can be passed within the URI's query string, specifying for example search terms, date ranges, or other information that defines the query. As part of a POST request, an arbitrary amount of data of any type can be sent to the server in a request message body.

如果你严格地使用GET(?)形式的url以这样的方式把你的json数据在查询字符串本身。

If you strictly have to use GET (?) form your url in such a way to put your json data in the query string itself.

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

更多推荐

libcurl http获取请求以json格式

本文发布于:2023-10-12 17:18:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1485361.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:格式   libcurl   http   json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!