C++ 如何执行GET编码JSON?

C++ 如何执行GET编码JSON?,c++,libcurl,C++,Libcurl,我想做的是使用GET方法,但使用JSON,使用参数和值执行CURL请求 我正在尝试执行以下操作: curl -X GET \ -H "X-Parse-Application-Id: 12345_Example" \ -H "X-Parse-REST-API-Key: abcde_Example" \ -G \ --data-urlencode "where={ \"pin\":\"A string\" }&

我想做的是使用GET方法,但使用JSON,使用参数和值执行CURL请求

我正在尝试执行以下操作:

curl -X GET \
-H "X-Parse-Application-Id: 12345_Example" \
-H "X-Parse-REST-API-Key: abcde_Example" \
-G \
--data-urlencode "where={ \"pin\":\"A string\" }" \
https://urlExample/classes/Pins
std::string temp = "where={ \"pin\":\"A string\" }";
char* encoded = curl_easy_escape(curl, temp.c_str(), temp.length());
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, std::strlen(encoded));
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, encoded);
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
如您所见,约束键值的
where
URL参数应编码为JSON

这是我的代码:

std::size_t callback(
    const char* in,
    std::size_t size,
    std::size_t num,
    char* out)
{
    std::string data(in, (std::size_t) size * num);
    *((std::stringstream*) out) << data;
    return size * num;
}

    public: Json::Value query(const char* serverAddress, const char* applicationId, const char* restAPIKey) {
        CURL* curl = curl_easy_init();
    
        curl_slist* headerlist = NULL;
        headerlist = curl_slist_append(headerlist, applicationId);
        headerlist = curl_slist_append(headerlist, restAPIKey);
    
        // Set HEADER.
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
    
        // Set remote URL.
        curl_easy_setopt(curl, CURLOPT_URL, serverAddress);
    
        // Don't bother trying IPv6, which would increase DNS resolution time.
        curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
    
        // Don't wait forever, time out after 10 seconds.
        curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);
    
        // Follow HTTP redirects if necessary.
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
    
        // Response information.
        int httpCode(0);
        std::stringstream httpData;
    
        // Hook up data handling function.
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback);
    
        // Hook up data container (will be passed as the last parameter to the
        // callback handling function).  Can be any pointer type, since it will
        // internally be passed as a void pointer.
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &httpData);
    
        // Run our HTTP GET command, capture the HTTP response code, and clean up.
        curl_easy_perform(curl);
        curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);
        curl_easy_cleanup(curl);
    
        if (httpCode == 200) {
            // Response looks good - done using Curl now. Try to parse the results.
            Json::Value jsonData;
            Json::CharReaderBuilder jsonReader;
            std::string errs;
    
            if (Json::parseFromStream(jsonReader, httpData, &jsonData, &errs)) {
                return jsonData["results"];
            }
            else {
                std::cout << "Could not parse HTTP data as JSON" << std::endl;
                std::cout << "HTTP data was:\n" << httpData.str() << std::endl;
                return NULL;
            }
        }
        else {
            std::cout << "Couldn't GET from " << serverAddress << " - exiting" << std::endl;
            return NULL;
        }
    }
但是没有成功。
libcurl
是否应该更新他们的API,并在本例中包含这样的功能?

好的——我将再次回答这个问题。这次是正确的。我掩盖了一个事实,即你在你的问题中张贴了文件。完全跳过了。不知道为什么我的大脑会这样。也许它讨厌文档,并本能地跳过它

所以,你的问题的答案很简单

保留您问题中的原始代码(完全忽略您在编辑中发布的代码,这是完全错误的),但不要这样做:

curl_easy_setopt(curl, CURLOPT_URL, serverAddress);
这样做:

const std::string whereQuery(curl_easy_escape(curl, "{ \"pin\":\"A string\" }", 0));
const std::string url("https://parseapi.back4app.com/classes/Pins?where=" + whereQuery);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());

很抱歉把它拖出来了。我需要更好地阅读问题。

(1)URL对JSON字符串进行编码。(2) 连接URL、
字符和步骤1的输出。(3) 使用步骤2的输出作为URL。@cdhowie我已经在那个解决方案中考虑过了,但是我正在寻找类似于
CURLOPT_POSTFIELDS
的东西,但是要找GET方法。据我所知,这个(
-G
)不是libcurl功能,它只是curl命令行功能。我不认为这是一个选择。@Simple--再一次,我为拖延道歉。如果你还有任何问题,请告诉我。其余的API看起来像是在发布
application/json
。您将不必“url编码”,因为它是请求的post数据的一部分。我感谢您的道歉,我接受它。再次感谢,现在我知道了,因为
POST
PUT
他们在
标题中使用了
内容类型:application/json
,所以我不会对其进行url编码。