Reputation: 8977
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.
Upvotes: 202
Views: 215341
Reputation: 185025
You can use the -o
switch and null
pseudo-file :
curl -s -o /dev/null -v http://google.com
curl -s -o nul -v http://google.com
Upvotes: 218
Reputation: 58480
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.
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
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
--silent
(-s
) suppresses the download-in-progress stats (but will still show HTML output)
--output /dev/null
(-o
) hides successful output
--show-error
(-S
) shows errors, even when silent
mode is enabled
--fail
(-f
) 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.
Upvotes: 106
Reputation: 35998
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
.
Upvotes: 19
Reputation: 42458
Another option to show the response headers and suppress the body:
curl -sD - https://example.com -o /dev/null
Upvotes: 1
Reputation: 1706
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:
output="$(curl -i ....)"
curl
argumentsUpvotes: 0
Reputation: 2168
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/
Upvotes: 72