• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6525
  • Last Modified:

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
0
pmtolk1
Asked:
pmtolk1
1 Solution
 
jkrCommented:
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


?
0
 
pmtolk1Author Commented:
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?
0
 
JimBeveridgeCommented:
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.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
pmtolk1Author Commented:
std::string ServerContent::DownloadJSON(std::string URL)
{
      
      CURL *curl;
      CURLcode res;
      struct curl_slist *headers=NULL; // init to NULL is important
      std::ostringstream oss;
       curl_slist_append(headers, "Accept: application/json");  
      curl_slist_append( headers, "Content-Type: application/json");
      curl_slist_append( headers, "charsets: utf-8");
       curl = curl_easy_init();

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

}
0
 
pmtolk1Author Commented:

 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;
}  
0
 
pmtolk1Author Commented:
Thanks guys for all your help, I finally got it working!!!
0
 
ken yupCommented:
i have question,in the solution,
 char *ct;    seems did not initial & delete,is it safe or will it be memory leak?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now