Question

How can you prevent / remove / stop the print/output to the command line in libcurl? For some reason, the following code works for URLs, but when I am hitting a page with an image, like http://192.168.123.123/banana.gif , then it crashes:

CURL *session;
session = curl_easy_init();
curl_easy_setopt(session, CURLOPT_URL, "http://192.168.123.123/banana.gif");

CURLcode curl_code = curl_easy_perform (session);
long http_code = 0;
curl_easy_getinfo(session, CURLINFO_RESPONSE_CODE, &http_code);
Was it helpful?

Solution

This is because you did not set the CURLOPT_WRITEDATA option:

The internal CURLOPT_WRITEFUNCTION will write the data to the FILE * given with this option, or to stdout if this option hasn't been set.

What you can do if you decide to completely ignore the response data is write it to /dev/null:

/* ... */

FILE *devnull = fopen("/dev/null", "w+");
curl_easy_setopt(session, CURLOPT_WRITEDATA, devnull);
CURLcode curl_code = curl_easy_perform(session);
fclose(devnull);

/* ... */

Another alternative is to use a NOOP write function:

curl_easy_setopt(session, CURLOPT_WRITEFUNCTION, noop_cb);

Where the write function simply returns the number of received bytes:

size_t noop_cb(void *ptr, size_t size, size_t nmemb, void *data) {
  return size * nmemb;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top