Function to read the request body and return it as a byte slice. It also restores the req.Body to be used again.
(req *http.Request)
| 307 | // Function to read the request body and return it as a byte slice. |
| 308 | // It also restores the req.Body to be used again. |
| 309 | func readAndRestoreRequestBody(req *http.Request) ([]byte, error) { |
| 310 | // Read the entire request body. |
| 311 | body, err := io.ReadAll(req.Body) |
| 312 | if err != nil { |
| 313 | return nil, err |
| 314 | } |
| 315 | // Restore the req.Body with a new reader for the original content. |
| 316 | req.Body = io.NopCloser(bytes.NewReader(body)) |
| 317 | |
| 318 | // Return the read body. |
| 319 | return body, nil |
| 320 | } |