How to get http server response in case of 401 using wget

httpwget

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.

Best Answer

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