如何执行GET编码的JSON?

问题描述

我想做的是使用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

如您所见,约束密钥值的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 @R_19_4045@ion.
        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;
        }
    }

我应该在我的代码包括什么才能使用编码的JSON执行GET方法

根据我正在使用的Server API的文档,当读取对象时,这就是卷曲的含义: back4app API Reference

阅读对象:

要检索对象,您需要向其类发送GET请求 在标题查询中具有您的应用程序凭据的端点 URL参数中的参数。这个任务很容易完成 只需调用您首选的Parse SDK的适当方法即可。 请在本文档的右侧面板中查看操作方法

请求网址https://parseapi.back4app.com/classes/Pins

方法GET

标题X-Parse-Application-Id: BCrUQVkk80pCdeimsXoKXL5ZCtyyEZwbN7mAb11f

X-Parse-REST-API-Key:swrFFIXJlFudtF3HkZPtfybDFRTmS7sPwvGUzQ9w

参数A,其中URL参数约束键的值。它 应该编码为JSON。

成功响应状态为200 OK

标头内容类型:application / json;

Body一个JSON对象,其中包含带有JSON数组的结果字段 列出对象。

编辑:

基于:Daniel Stenberg's answer,我尝试了以下操作:

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");

但是没有成功。 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,url.c_str());

很抱歉将其拖出。我需要更好地阅读问题。