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

- 163,903
- 24
- 228
- 223

- 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 Answers
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

- 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
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.

- 54,736
- 17
- 146
- 222

- 41,996
- 18
- 86
- 115
-
Thanks, but I know this. There isn't a way without "deviate" the output (a way to delete it)? – stdio May 11 '10 at 23:41
-
1@stdio - If you just want the input to go away, open a NULL device and print everything there. – Tim Post May 12 '10 at 00:27
-
2@Tm Post: do you mean /dev/null? if you mean this, the code would not be multiplatform. – stdio May 12 '10 at 00:48
-
2FILE* devnull = fopen("nul", "w"); curl_easy_setopt(curl, CURLOPT_WRITEDATA, devnull); – Alexx Roche Nov 19 '15 at 15:55
-
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).