15

How can I hide curl_easy_perform output (in a shell)?
This is in regards to a C application.

mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
stdio
  • 435
  • 2
  • 7
  • 18
  • Curl_easy_perform prints the page content returned in response to a request (post request in my case). I want "hide" that page content (no view) and print my personal message – stdio May 11 '10 at 23:24

3 Answers3

25

Use CURLOPT_NOBODY in curl_easy_setopt(). Example:

 ...

CURL *curl;
CURLcode statusCode;

curl = curl_easy_init();
if(curl){
    curl_easy_setopt(curl, CURLOPT_URL, "http://www.google.com/");
    //CURLOPT_NOBODY does the trick
    curl_easy_setopt(curl, CURLOPT_NOBODY, 1);
    curl_easy_perform(curl);

 ...

Link to docs: http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTNOBODY

Paul Semionov
  • 491
  • 6
  • 17
  • I wanted to log into a page https://user:pass@www.example.com/login/ and then use curl to POST to https://www.example.com/upload/ but with CURLOPT_NOBODY I lose the session-id. I solved this by writing to nul – Alexx Roche Nov 19 '15 at 15:59
  • Am I right to suppose this is useful for POST requests, but probably not so for GET? – xtofl Aug 29 '17 at 09:18
  • for POST requests, this simply does not send the body, meaning it's not what you would expect. – xtofl Aug 29 '17 at 09:31
15

Set the CURLOPT_WRITEFUNCTION and/or CURLOPT_WRITEDATA options:

FILE *f = fopen("target.txt", "wb");
curl_easy_setopt(handle, CURLOPT_WRITEDATA, f);

By default, libcurl writes output to stdout. When you override this (which is what almost any application will do), it will write to another file or to pass chunks of output to a callback. See the documentation for CURLOPT_WRITEFUNCTION for more details.

Daniel Stenberg
  • 54,736
  • 17
  • 146
  • 222
Joey Adams
  • 41,996
  • 18
  • 86
  • 115
7

As Joey said, CURLOPT_WRITEFUNCTION will allow you to completely disregard all output. Just set up a callback that does absolutely nothing if you want the data to just go away, without being written to any file descriptor.

For instance,

/* Never writes anything, just returns the size presented */
size_t my_dummy_write(char *ptr, size_t size, size_t nmemb, void *userdata)
{
   return size * nmemb;
}

Then in your options:

curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, &my_dummy_write);

Or, point the file handle at a NULL device (a lot easier).

Community
  • 1
  • 1
Tim Post
  • 33,371
  • 15
  • 110
  • 174