1

I am developing on Linux platform. I am using libcurl and able to receive json response and saving it to file. Below is the code.

#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
#include <string.h>

#define URL "http://www.joes-hardware.com/tools.html"
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
    size_t written;
    written = fwrite(ptr, size, nmemb, stream);
    return written;
}

int main(void) {
    CURL *curl;
    FILE *fp;
    CURLcode res;
    //const char url[] = "http://www.joes-hardware.com/tools.html";

    char *url= URL;
    char outfilename[FILENAME_MAX] = "./json";
    curl = curl_easy_init();
    if (curl) {
        fp = fopen(outfilename,"wb");
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
        fclose(fp);
    }
    return 0;
}

Now I need to fetch a zip file from the server. Suppose the URL is of the format shown below:

#define URL "https://Server/File.zip"

For such URL, the code is not able to save the zip file.

How to achieve this?

jww
  • 97,681
  • 90
  • 411
  • 885
Gaurav K
  • 2,864
  • 9
  • 39
  • 68
  • Did you check the result code of *every* api you're calling in this list to see if any of them fail? In particular, the `fopen` and the `curl_easy_perform`, but *all* of them isn't a bad idea. And the setup for an `https` url can be considerably more complicated than a simple pull. [see this](http://curl.haxx.se/libcurl/c/simplessl.html). – WhozCraig Aug 28 '14 at 04:36
  • @WhozCraig Ya the code works absolutely fine for `http` request say.... `"http://www.joes-hardware.com/tools.html"`.. It saves the json response to respective file.. but request `https://Server/File.zip` is not saved – Gaurav K Aug 28 '14 at 04:48

1 Answers1

2

I resolved the issue. The problem was with HTTPS connection. I had to add certificates for HTTPS.

Based on below links:

#define CURL_STATICLIB
#include <stdio.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
#include <string.h>
#include <stdlib.h>

#define false 0

size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
    size_t written;
    written = fwrite(ptr, size, nmemb, stream);
    return written;
}

int main(void) {
    CURL *curl;
    FILE *fp;
    CURLcode res;

    const char url[] = "https://example.com/filename.zip";
    const char outfilename[FILENAME_MAX] = "./json.zip";

    curl_version_info_data * vinfo = curl_version_info(CURLVERSION_NOW);

    if(vinfo->features & CURL_VERSION_SSL){
        printf("CURL: SSL enabled\n");
    }else{
        printf("CURL: SSL not enabled\n");
    }

    curl = curl_easy_init();
    if (curl) {
        fp = fopen(outfilename,"wb");        

        /* Setup the https:// verification options. Note we   */
        /* do this on all requests as there may be a redirect */
        /* from http to https and we still want to verify     */
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_CAINFO, "./ca-bundle.crt");
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);

        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);

        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);

        int i=fclose(fp);
        if( i==0)
        system("unzip -j json.zip");
    }
    return 0;
}
jww
  • 97,681
  • 90
  • 411
  • 885
Gaurav K
  • 2,864
  • 9
  • 39
  • 68
  • I wonder why this doesn't work neither for DropBox nor for Google Driver public links.Do you have an idea? – Michael IV Feb 26 '18 at 19:51
  • `curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);` is a bad idea. You should fix your certificate and/or chain problem. Also see the docs for [`CURLOPT_SSL_VERIFYHOST`](https://curl.haxx.se/libcurl/c/CURLOPT_SSL_VERIFYHOST.html). – jww Nov 02 '19 at 02:42