readResponseBody consumes the body of the response. readResponseBody returns an informational message about the disposition of the response body's contents.
(req *http.Request, resp *http.Response)
| 447 | // readResponseBody returns an informational message about the |
| 448 | // disposition of the response body's contents. |
| 449 | func readResponseBody(req *http.Request, resp *http.Response) string { |
| 450 | if isRedirect(resp) || req.Method == http.MethodHead { |
| 451 | return "" |
| 452 | } |
| 453 | |
| 454 | w := io.Discard |
| 455 | msg := color.CyanString("Body discarded") |
| 456 | |
| 457 | if saveOutput || outputFile != "" { |
| 458 | filename := outputFile |
| 459 | |
| 460 | if saveOutput { |
| 461 | // try to get the filename from the Content-Disposition header |
| 462 | // otherwise fall back to the RequestURI |
| 463 | if filename = getFilenameFromHeaders(resp.Header); filename == "" { |
| 464 | filename = path.Base(req.URL.RequestURI()) |
| 465 | } |
| 466 | |
| 467 | if filename == "/" { |
| 468 | log.Fatalf("No remote filename; specify output filename with -o to save response body") |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | f, err := os.Create(filename) |
| 473 | if err != nil { |
| 474 | log.Fatalf("unable to create file %s: %v", filename, err) |
| 475 | } |
| 476 | defer f.Close() |
| 477 | w = f |
| 478 | msg = color.CyanString("Body read") |
| 479 | } |
| 480 | |
| 481 | if _, err := io.Copy(w, resp.Body); err != nil && w != io.Discard { |
| 482 | log.Fatalf("failed to read response body: %v", err) |
| 483 | } |
| 484 | |
| 485 | return msg |
| 486 | } |
| 487 | |
| 488 | type headers []string |
| 489 |
no test coverage detected