Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4535462
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T14:24:29+00:00 2026-05-21T14:24:29+00:00

I am trying to download a json file from a REST webpage in C++

  • 0

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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T14:24:30+00:00Added an answer on May 21, 2026 at 2:24 pm
    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;
    
    }   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to download and save a file from a HttpWebResponse but im having
I'm trying to access the REST Disqus API using the following url: http://disqus.com/api/3.0/threads/listPosts.json ?api_key=myKey
I am trying to download a file from a website using python and mechanize.
I'm trying to download the XLS file from this page: http://www.nordpoolspot.com/Market-data1/Elspot/Area-Prices/ALL1/Hourly/ (click on Export
I am trying to download a file from server, what I have in mind
I am trying download file http://images.anandtech.com/doci/5434/X79%20Extreme9Box_575px.jpg but not able to download it using HttpUrlConnection,
I am trying to download some JSON from the google book API. The URL
I'm trying to fetch information from a JSON web service with this code, using
I'm trying to grab data from a JSON on an external site but the
i am trying to download a json file in a intentservice and when the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.