177

Is it possible instruct cURL to suppress output of response body?

In my case, the response body is an HTML page, which overflows the CLI buffer, making it difficult to find the relevant information. I want to examine the other parts of the output such as HTTP response code, headers, e.t.c. - everything except the actual HTML.

d4nyll
  • 11,811
  • 6
  • 54
  • 68
BaltoStar
  • 8,165
  • 17
  • 59
  • 91

6 Answers6

192

You can use the -o switch and null pseudo-file :

Unix

curl -s -o /dev/null -v http://google.com

Windows

curl -s -o nul -v http://google.com
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
  • 1
    ... if you are on Unix ;-) For Windows you might want to use nul. Here is the link to the docs, perhaps BaltoStar feels the urge to read before asking next time ;-) http://curl.haxx.se/docs/manpage.html – Marged Sep 09 '15 at 20:08
  • 3
    You are welcome. But it is "nul", "null" will create a file with that name – Marged Sep 09 '15 at 20:15
  • 1
    in fact i did read the man page before posting and tried `-s -o nul` but on my windows machine `curl -s -o nul -v http://{internal-host}/{internal-uri}` outputs page's entire html -- i tried `curl -s -o nul -v http://google.com` and it does suppress output as expected , so i'm not understanding why this doesn't work for my internal site – BaltoStar Sep 10 '15 at 03:07
  • 1
    `-o` only specifies one URLs output: if you have multiple urls, it will only discard the first :( – Simon Buchan Oct 13 '17 at 03:38
  • 4
    Relies on non-portable `NUL` and `/dev/null`. Using [`-sIXGET`](https://stackoverflow.com/a/49669324/429091) is a better option IMO. – binki Aug 15 '18 at 15:05
89

Here's a way to suppress all curl output and headers, with the option of still showing errors if they occur. Useful for cron jobs or automated testing.

Unix

To suppress all output:

curl --silent --output /dev/null http://example.com

To suppress output but still show errors if they occur:

curl --silent --output /dev/null --show-error --fail http://example.com

Windows

To suppress all output:

curl --silent --output nul http://example.com

To suppress output but still show errors if they occur:

curl --silent --output nul --show-error --fail http://example.com

Parameters Explained

--silent suppresses the download-in-progress stats (but will still show HTML output)
--output /dev/null hides successful output
--show-error shows errors, even when silent mode is enabled
--fail will raise an error if HTTP response is an error code (404, 500 etc.) instead of merely DNS/TCP errors.


UPDATE: I realise the original author wanted to inspect the headers and response code of a request rather than silencing everything. See samael's answer for details on how to do that.

Simon East
  • 55,742
  • 17
  • 139
  • 133
  • 3
    Relies on non-portable `NUL` and `/dev/null`. Using [`-sIXGET`](https://stackoverflow.com/a/49669324/429091) is a better option IMO. – binki Aug 15 '18 at 15:05
  • 1
    Thanks for the comment @binki - they actually do different things. Also realised the OP wanted to inspect the headers which don't show in my solution. I've updated my answer to reflect that. – Simon East Aug 24 '18 at 00:09
  • 1
    I guess in rereading the OP’s post they didn’t say they *only* wanted to see headers. They might actually have been looking for `curl -vo/dev/null`/`curl -voNUL`… and I don’t know if there is a portable variant of that, hrm… ;-). – binki Aug 24 '18 at 01:52
  • Is there a short command for --fail? I think -sS is what we need for both --silent and --show-error – Freedo Aug 03 '19 at 18:09
  • 2
    Yes, there is: `curl -Ssfo /dev/null url`. But be nice to the future maintainers. – Amit Naidu Aug 14 '20 at 04:15
  • Yes! Finally an answer that works! – Robbert van den Bogerd Nov 26 '21 at 11:12
60

When you want to show headers but hide the response body, you'll want to use:

curl -sIXGET http://somedomain.com/your/url

I'd been using curl -I http://somedomain.com/your/url for just showing response headers. The problem with that though is that it makes the request using the HEAD method which is no good when you want to test an API call that only responds to a GET request. This is what the -X GET is for, it changes the request to a GET.

So, in summary:

-s hides the progress bars from output (especially useful when piping to another program)
-I shows headers (but makes a HEAD request)
-XGET converts request back to a GET request

see: http://www.woolie.co.uk/article/curl-full-get-request-dropping-body/

Simon East
  • 55,742
  • 17
  • 139
  • 133
samael
  • 2,007
  • 1
  • 16
  • 34
  • 2
    That’s really complicated; `curl -i` does exactly the same thing. – bfontaine Dec 07 '18 at 14:39
  • 11
    no, `-i` includes the HTTP-header in the output as well as the body. Try it. `curl -i http://google.com` is not the same as `curl -IXGET http://google.com` – samael Dec 07 '18 at 16:18
  • For my purposes (and the OP's) `curl -I url` is sufficient and has the same effect. I just needed to check if the url was responsive. This question too was merely about suppressing the body, which `HEAD` does well. – Amit Naidu Aug 14 '20 at 04:16
  • 4
    But HEAD / GET may get you a different result. It is not the same. Especially if you need these type of commands to troubleshoot what is happening. I found the suggestion in this answer very helpful. -i and -I is not the same. -I and -sIXGET is not the same. – Sybille Peters Sep 15 '21 at 09:05
  • 1
    `curl -sIG http://...` should also be equivalent to `-sIXGET` – Miles Libbey Jun 06 '23 at 18:35
  • `-G` is not equivalent to `-XGET` - in fact `-IG` will make a HEAD request. – cmbuckley Jun 15 '23 at 08:29
13

Just make a HEAD request. You will get the headers without the body. A standards-compilant server is supposed to send exactly the same information here as it would to a GET request.

curl --head <url>

Alternatively, if a HEAD request doesn't work for you for some reason, the following will make cURL send a GET request but then print response code and headers and drop the connection without receiving the response body -- unlike other answers which receive and then discard it. This can save a lot of time and bandwidth, especially if the body is very large.

curl --head -X GET <url>

You can do likewise with any other verb (e.g. POST) by supplying it to the -X option instead of GET.

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
  • This doesn't works for POST, and also depends on the implementation of HEAD in the server – Allan Deamon May 24 '21 at 02:19
  • @AllanDeamon doesn't `-X POST` work? `-X` overrides the verb sent but keeps Curl's behavior from `--head` -- receive and show just the response code and headers. – ivan_pozdeev May 24 '21 at 15:27
  • The server implements the HEADER. I saw many implementations that do not work as intended. But the point here is that the HEADER is equivalent to GET. The correct way to implement is to do everything GET does and then just send the headers. So if you want to trigger the behavior or another verb like POST, the HEAD verb won't work. So when you use --head, instead of sending a GET it sends a HEAD. But you cannot send 2 HTTP verbs. – Allan Deamon May 26 '21 at 16:32
  • 1
    @AllanDeamon Maybe I wasn't clear enough. `curl --head -X ` sends ``, not `HEAD`. – ivan_pozdeev May 26 '21 at 19:10
  • Yeah, in that case, I suggest you edit the answer to make it more clear, as It seems true to be a valid answer, but other people could have the same misunderstanding as me. – Allan Deamon May 26 '21 at 21:24
  • @AllanDeamon Done. – ivan_pozdeev May 26 '21 at 22:53
  • I just hit a situation where I normally use `-I` to view the headers and in this case a GET gives me 200/OK and a HEAD request gives me 500/Server Error, so -IXGET was helpful for me. – Geoffrey Wiseman Jan 27 '23 at 15:28
1

Another option to show the response headers and suppress the body:

curl -sD - https://example.com -o /dev/null
cmbuckley
  • 40,217
  • 9
  • 77
  • 91
0

I would suggest the following solution:

curl -sS -i https://example.com | perl -e '$p = 1; while(<STDIN>){ $p = 0 if $_ =~ /^\s*$/; print if $p }'

It's a longish one-liner but does what you need:

  • the body is suppressed
  • the headers & response codes are output @ stdout, so that you can also pipe the info to another command or capture it in a shell variable like output="$(curl -i ....)"
  • any cURL errors are sent @ stderr
  • this works for both GET & POST requests, as well as for any other HTTP request methods, and you can use the other standard curl arguments
famzah
  • 1,462
  • 18
  • 21