1

I am trying to make a HTTP Request to retrieve some JSON data; I get the error that curl variable is not initialized though I easy_init() it. Any help on how to go around this error would be very kind!!

Below is my code:

#pragma once
#include "stdafx.h"
#include "RequestJson.h"
#include <string.h>
#include <include/curl/curl.h>
#include <fstream>
#include <iostream>
#include <sstream> 


using namespace std;
class RequestJson
{
public:

    static std::string RequestJsonString(std::string URL)
    {
        //set to get the JSON Response on listed loans; open a CSV file and read unemployment and other indices.

        ::CURL *curl;
        CURLcode res;
        struct curl_slist *headers = NULL;
        std::ostringstream oss;

        //curl_global_init(CURL_GLOBAL_ALL);
        curl = curl_easy_init();
        curl_slist_append(headers, "Accept: application/json");
        curl_slist_append(headers, "Content-Type: application/json");
        curl_easy_cleanup(curl);        

        if (curl)
        {

            curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
            curl_easy_setopt(curl, CURLOPT_URL, URL.c_str());
            curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
            curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
            curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer); //define a write-function below. 
            res = curl_easy_perform(curl);
            if (CURLE_OK == res)
            {
                char *ct;
                res = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);
                if ((CURLE_OK == res) && ct)
                    {
                    return *DownloadedResponse;
                    }
            }
        }
    }

    //parse the JSON String and return the downloaded string. 
    static std::string *DownloadedResponse;
    static int writer(char *data, size_t size, size_t nmemb, std::string *buffer_in)
    {
        if (buffer_in != NULL)
        {
            buffer_in->append(data, size * nmemb);
            DownloadedResponse = buffer_in;
            return size * nmemb;
        }

        return 0;
    }


};
6
  • The "error" is probably just a warning, but it would help if you could tell us where you get the "error". Please edit your question so, for example, the code includes comments on the lines of the "errors". Also please include the actual build output in the body of the question. And if you don't get it as build errors/warning, do you get it as runtime crashes? Commented May 9, 2016 at 9:40
  • Thank you Joachim for your reply, sorry I just noticed I was cleaning up before the request, I've moved the clean up towards the end of the function. Now the unresolved externals errors persists. I've edited the code above and included the build output. Commented May 9, 2016 at 9:47
  • So you fixed the problem you was originally asking about, but now have other problems? Then those other problems should be posted as another question. Or is the "unresolved external" error you're calling "curl variable not initialized"? Please read about how to ask good questions. Commented May 9, 2016 at 9:50
  • Thank you. I' ll redit this question to show the first error and post a new one. Commented May 9, 2016 at 9:51
  • Lastly, why is DownloadedResponse a pointer? Where do you define the variable? Where do you initialize (allocate memory for it)? And where do you set the CURLOPT_WRITEDATA option? All that looks like an undefined behavior in waiting. Commented May 9, 2016 at 9:55

1 Answer 1

1

From the curl_easy_cleanup reference:

This function must be the last function to call for an easy session. It is the opposite of the curl_easy_init function and must be called with the same handle as input that a curl_easy_init call returned.

[Emphasis mine]

When you call curl_easy_cleanup it cleans up all resources allocated by curl_easy_init. After that you can't use the CURL pointer any more.

As the reference says: Put it last, when you're done.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.