GetBody reads and returns the request body.
(r *http.Request, resetBody bool)
| 2635 | |
| 2636 | // GetBody reads and returns the request body. |
| 2637 | func GetBody(r *http.Request, resetBody bool) ([]byte, func(), error) { |
| 2638 | data, err := io.ReadAll(r.Body) |
| 2639 | if err != nil { |
| 2640 | return nil, nil, err |
| 2641 | } |
| 2642 | |
| 2643 | if resetBody { |
| 2644 | // * remember, Request.Body has no Bytes(), we have to consume them first |
| 2645 | // and after re-set them to the body, this is the only solution. |
| 2646 | return data, func() { |
| 2647 | setBody(r, data) |
| 2648 | }, nil |
| 2649 | } |
| 2650 | |
| 2651 | return data, emptyFunc, nil |
| 2652 | } |
| 2653 | |
| 2654 | func setBody(r *http.Request, data []byte) { |
| 2655 | r.Body = io.NopCloser(bytes.NewBuffer(data)) |
no test coverage detected
searching dependent graphs…