Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

read json with curl in c++

Posted on 2011-04-20
7
Medium Priority
?
5,916 Views
Last Modified: 2014-12-30
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
Comment
Question by:pmtolk1
7 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 35434737
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
 
LVL 2

Author Comment

by:pmtolk1
ID: 35434828
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
 
LVL 7

Expert Comment

by:JimBeveridge
ID: 35460832
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 2

Accepted Solution

by:
pmtolk1 earned 0 total points
ID: 35460887
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
 
LVL 2

Author Comment

by:pmtolk1
ID: 35460927

 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
 
LVL 2

Author Closing Comment

by:pmtolk1
ID: 35496445
Thanks guys for all your help, I finally got it working!!!
0
 

Expert Comment

by:ken yup
ID: 40523681
i have question,in the solution,
 char *ct;    seems did not initial & delete,is it safe or will it be memory leak?
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question