curl / Mailing Lists / curl-library / Single Mail
Buy commercial curl support from WolfSSL. We help you work out your issues, debug your libcurl applications, use the API, port to new platforms, add new features and more. With a team lead by the curl founder himself.

Performing a GET request with JSON

From: rafa via curl-library <curl-library_at_cool.haxx.se>
Date: Wed, 19 Aug 2020 21:07:54 -0300

What I want to do is to perform CURL request with parameters and values by
using GET method but using JSON.

I'm trying to perform the following:

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

as you can see the where URL parameter constraining the value for keys
should be encoded JSON.

This is my code:

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

        std::string temp = "{ \"pin\":\"A string\" }";
        curl_easy_setopt(curl, CURLOPT_POST, 1L);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, temp.size());
        curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, temp.c_str());
        curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");

        // 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;
        }
    }

What should I include in my code in order to perform the GET method with
encoded JSON?
According to the documentation of the Server API I'm using, when reading
objects, this is what it says for curl: back4app API Reference
<https://dashboard.back4app.com/apidocs#Pins-reading-objects>

> *READING OBJECTS:*To retrieve an object, you'll need to send a *GET
> request* to its class endpoint with your app's credentials in the headers
> and the query parameters in the URL parameters. This task can be easily
> accomplished just by calling the appropriated method of your preferred
> Parse SDK. Please check how to do it in the right panel of this
> documentation.
> Request URL https://parseapi.back4app.com/classes/Pins
> *Method GET*
> Headers X-Parse-Application-Id: BCrUQVkk80pCdeImSXoKXL5ZCtyyEZwbN7mAb11f
> X-Parse-REST-API-Key: swrFFIXJlFudtF3HkZPtfybDFRTmS7sPwvGUzQ9w
> Parameters* A where URL parameter constraining the value for keys. It
> should be encoded JSON.*
> Success Response Status 200 OK
> Headers content-type: application/json;
> Body a JSON object that contains a results field with a JSON array that
> lists the objects.

Based on: Daniel Stenberg's answer
<https://curl.haxx.se/mail/lib-2007-12/0099.html> I tried the following:

std::string temp = "{ \"pin\":\"A string\" }";
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, temp.size());
curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, temp.c_str());
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");

But no success. Should libcurl update their API and include such a
feature for this case?

You can also see that question on: stackoverflow
<https://stackoverflow.com/questions/63444328/how-to-perform-get-encoded-json>
for a better text formatting.

<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=icon>
Virus-free.
www.avast.com
<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=link>
<#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>

-------------------------------------------------------------------
Unsubscribe: https://cool.haxx.se/list/listinfo/curl-library
Etiquette: https://curl.haxx.se/mail/etiquette.html
Received on 2020-08-20