@@ -264,19 +264,20 @@ func DumpRequest(req *http.Request, body bool) (dump []byte, err error) {
264
264
return
265
265
}
266
266
267
- // errNoBody is a sentinel error value used by failureToReadBody so we can detect
268
- // that the lack of body was intentional.
267
+ // errNoBody is a sentinel error value used by failureToReadBody so we
268
+ // can detect that the lack of body was intentional.
269
269
var errNoBody = errors .New ("sentinel error value" )
270
270
271
271
// failureToReadBody is a io.ReadCloser that just returns errNoBody on
272
- // Read. It's swapped in when we don't actually want to consume the
273
- // body, but need a non-nil one, and want to distinguish the error
274
- // from reading the dummy body.
272
+ // Read. It's swapped in when we don't actually want to consume
273
+ // the body, but need a non-nil one, and want to distinguish the
274
+ // error from reading the dummy body.
275
275
type failureToReadBody struct {}
276
276
277
277
func (failureToReadBody ) Read ([]byte ) (int , error ) { return 0 , errNoBody }
278
278
func (failureToReadBody ) Close () error { return nil }
279
279
280
+ // emptyBody is an instance of empty reader.
280
281
var emptyBody = ioutil .NopCloser (strings .NewReader ("" ))
281
282
282
283
// DumpResponse is like DumpRequest but dumps a response.
@@ -286,7 +287,13 @@ func DumpResponse(resp *http.Response, body bool) (dump []byte, err error) {
286
287
savecl := resp .ContentLength
287
288
288
289
if ! body {
289
- resp .Body = failureToReadBody {}
290
+ // For content length of zero. Make sure the body is an empty
291
+ // reader, instead of returning error through failureToReadBody{}.
292
+ if resp .ContentLength == 0 {
293
+ resp .Body = emptyBody
294
+ } else {
295
+ resp .Body = failureToReadBody {}
296
+ }
290
297
} else if resp .Body == nil {
291
298
resp .Body = emptyBody
292
299
} else {
0 commit comments