c++ - libcurl uninitialized variable curl error -
i trying make http request retrieve json data; error curl variable not initialized though easy_init() it. on how go around error kind!!
below 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 json response on listed loans; open csv file , read unemployment , 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 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 json string , return 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; } };
from the curl_easy_cleanup
reference:
this function must the last function call easy session. opposite of curl_easy_init function , must called same handle input curl_easy_init call returned.
[emphasis mine]
when call curl_easy_cleanup
cleans up resources allocated curl_easy_init
. after can't use curl
pointer more.
as reference says: put last, when you're done.
Comments
Post a Comment