11

I need to test my http server responses in various cases, even for failed authentication. In case of failed authentication, my server returns 401 Unauthorized and also a response body which simple contains Unauthorized (or maybe some other detailed message).

Using e.g. curl or httpie, I got those response body in case of 401 response.

$ curl http://10.5.1.1/bla 
Unauthorized
$ curl http://10.5.1.1/bla --digest --user joe:wrong 
Unauthorized
$ http http://10.5.1.1/bla -b
Unauthorized
$ http http://10.5.1.1/bla -b --auth-type digest --auth joe:wrong
Unauthorized

But when trying this using wget, I got no output:

$ wget http://10.5.1.1/bla -q -O /dev/stdout
$ wget http://10.5.1.1/bla -q -O /dev/stdout --user joe --password wrong

wget returns with exitcode 6 in this case, but I need to check the response message.

Here is a dump of the complete traffic, captured using httpie:

$ http http://10.5.1.1/bla --print hbHB
GET /bla HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: 10.5.1.1
User-Agent: HTTPie/0.9.2

HTTP/1.1 401 Unauthorized
Connection: keep-alive
Content-Length: 13
Content-Type: text/plain
Date: 2017-08-27 23:01:07
Server: Vistra-I St10 SW218 HW010
WWW-Authenticate: Digest realm="Vistra-I", qop="auth", nonce="a0c5d461f2b74b2b797b62f54200d125", opaque="0123456789abcdef0123456789abcdef"

Unauthorized

(Note that the Unauthorized message in the response body ends with a new line character, that's why Content-Length: 13)

Same thing, if the server respond with 403 or 404 et cetera. Any idea how to get the response body using wget in this case?

Edit 2017-09-22

Found --content-on-error option in my wget 1.17.1 (see also wget manual).

This works in case of e.g. response code 404 but not for 401 nor 5xx codes.

For 401 see this bug.

3 Answers 3

7

I was able to obtain server response in case of 404, 503 errors with wget 1.20.1 with the following command:

wget --no-verbose --save-headers --content-on-error=on --timeout=3 --tries=1 -O- http://127.0.0.1:8080/bla

Example output:

HTTP/1.1 404 Not Found
X-Endpoint-Path: GET /bla
X-Sub-Calls-Count: 0
X-Mdc: XNIO-1 task-11550846786294
X-Child-Calls-Count: 0
X-Hop: 1
Date: Fri, 22 Feb 2019 14:46:26 GMT
Connection: keep-alive
Transfer-Encoding: chunked
Content-Type: application/json;charset=UTF-8

{"id":"266a8c43-81ae-4b73-bb0c-d3e0a37a11c4","createdAt":1550846786327,"httpCode":404,"code":"RESOURCE_NOT_FOUND","message":"RESTEASY003210: Could not find resource for full path: http://127.0.0.1:8080/bla"}http://127.0.0.1:8080/bla:
2019-02-22 15:46:26 ERROR 404: Not Found.
2
  • 1
    Doesn't work in BusyBox v1.29.3: wget: unrecognized option: content-on-error=on
    – Muhd
    Sep 24, 2019 at 3:30
  • Yes, it works for 400. But how to see the content on redirect 302? Mar 16, 2022 at 10:20
0

Response header can be retrieved using the following wget option

wget --server-response http://10.5.1.1/bla 

I'm not aware of any optino that will allow you to retrieve the response body when an error happens using wget

In case you want the response body with another command, use curl as is:

curl http://10.5.1.1/bla
4
  • 1
    This gives me the response header but not the response body
    – Joe
    Sep 21, 2017 at 13:29
  • What about curl http://10.5.1.1/bla ?
    – SOKS
    Sep 21, 2017 at 13:52
  • Yes, it works with both curl and httpie, but I want to make it also work with wget (if possible).
    – Joe
    Sep 21, 2017 at 14:42
  • Not sure you can with wget
    – SOKS
    Sep 21, 2017 at 16:46
0

In my case, with "403 Forbidden" I have to use --content-on-error as the other answers say, but for some reason I only see the error message if I also use -q. So the trick is using both:

wget -q --content-on-error -O- http://...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .