Link to home
Start Free TrialLog in
Avatar of pmtolk1
pmtolk1Flag for Afghanistan

asked on

read json with curl in c++

Hi all,
I am trying to download a json file from a REST webpage in C++ with curl. The following code works if I go to the webpage but it doesnt download, or I dont know where the json is stored, if I try to access the json ....

I think it should be an easy fix but I cant find any reference to this ...

If I go to webpage it opens the json but this code only returns

text/html; charset=utf-8

	CURL *curl;
	CURLcode res;
	struct curl_slist *headers=NULL; // init to NULL is important 
    headers = curl_slist_append(headers, "Accept: application/json");  
 
	curl = curl_easy_init();
	if(curl) {
		curl_easy_setopt(curl, CURLOPT_URL, "http://web.com/api/json/123");//cant get json file
		curl_easy_setopt(curl, CURLOPT_URL, "http://web.com/pages/123.html");//this returns entire webpage
		curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
		curl_easy_setopt(curl, CURLOPT_RETURNTRANSFER, true);
	    res = curl_easy_perform(curl);

		if(CURLE_OK == res) {
			char *ct;
			// ask for the content-type
		    res = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);
			if((CURLE_OK == res) && ct)
		        printf("We received Content-Type: %s\n", ct);
		}
	}
    // always cleanup  
    curl_easy_cleanup(curl);

Open in new window


also I should note that the following command line option works
curl -i -H "Accept: application/json" -X GET http://web.com/api/json/123
Avatar of jkr
jkr
Flag of Germany image

Does it help if you make that
 
headers = curl_slist_append(headers, "Accept: application/json");  
              curl_slist_append( headers, "Content-Type: application/json");
              curl_slist_append( headers, "charsets: utf-8");

Open in new window


?
Avatar of pmtolk1

ASKER

The server is still returning text/html; charset=utf-8 from this line  res = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);

How can I get the actual json?
You can use Firebug in Firefox to monitor the exact http data that's going back and forth. After you install Firebug, press F12 to open it, then click on Net. Open the page in the normal way and you should see the http data that was sent and received.

Then you need to get a log of the data sent and received by libcurl to see how it's different. Under Windows you could also use a tool like httptracer to inspect the actual data, but I'm not sure how to do that under Linux.
ASKER CERTIFIED SOLUTION
Avatar of pmtolk1
pmtolk1
Flag of Afghanistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pmtolk1

ASKER


 static std::string *DownloadedResponse;

static int writer(char *data, size_t size, size_t nmemb, std::string *buffer_in)
{
      // Is there anything in the buffer?
      if (buffer_in != NULL)
      {
            // Append the data to the buffer
            buffer_in->append(data, size * nmemb);
            // How much did we write?
            DownloadedResponse = buffer_in;
            return size * nmemb;
      }
      return 0;
}  
Avatar of pmtolk1

ASKER

Thanks guys for all your help, I finally got it working!!!
Avatar of ken yup
ken yup

i have question,in the solution,
 char *ct;    seems did not initial & delete,is it safe or will it be memory leak?