10

I am trying to download a json file from a REST webpage in C++ with libcurl. The following code works if I go to the webpage but it doesnt download 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");
            curl_easy_setopt(curl, CURLOPT_HTTPGET,1);
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
    //curl_easy_setopt(curl, CURLOPT_URL, "http://web.com/123.html");//this works!!!
    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);
mpromonet
  • 11,326
  • 43
  • 62
  • 91
Philip
  • 579
  • 3
  • 8
  • 19

3 Answers3

20
std::string ServerContent::DownloadJSON(std::string URL)
{   
    CURL *curl;
    CURLcode res;
    struct curl_slist *headers=NULL; // init to NULL is important 
    std::ostringstream oss;
    headers = curl_slist_append(headers, "Accept: application/json");  
    headers = curl_slist_append(headers, "Content-Type: application/json");
    headers = curl_slist_append(headers, "charset: 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_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;
        }
    }

    curl_slist_free_all(headers);
}


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;

}   
Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141
Philip
  • 579
  • 3
  • 8
  • 19
1

I think this has to do with the configuration on the HTTP server. First, you should be sending some type of indication of what type you're expecting, for example, adding an Accept header like this:

Accept: application/json

If you don't specify what you're expecting, the server may return the default HTML in the response header Content-Type, and that's what curl says to you.

Diego Sevilla
  • 28,636
  • 4
  • 59
  • 87
  • I tried to add the following but I cant get to the actually data from the server response.... struct curl_slist *headers=NULL; // init to NULL is important headers = curl_slist_append(headers, "Accept: application/json"); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); – Philip Apr 20 '11 at 16:38
0

Have you tried this approach from the tutorial code at the curl site? http://curl.haxx.se/libcurl/c/sepheaders.html

holtavolt
  • 4,378
  • 1
  • 26
  • 40
  • I want to read the json not put it, is there a CURLOPT_READHEADER or something to read the stream of data returned from server? btw this worls for me on the command line curl -i -H "Accept: application/json" -X GET web.com/api/json/123 – Philip Apr 18 '11 at 23:24