If you receive a http request it may be a http response? In that case I do like this (depending on the desired format):
// read body into to map
var result map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(result)
Sorry for the less explanation, I receive a http request from the client, using its body, I create a new http request and send it to a third party api. I am redirecting the incoming request’s body to the third party.
When I get a request from the client (GET, POST etc) , I read the body in about the same way.
// Read the JSON data from the request body
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Error reading request body", http.StatusBadRequest)
return
}
After analysis, I understand that the current method for handling memory was not ideal. Switching to httputil.ReverseProxy resulted in a substantial improvement, decreasing memory usage from gigabytes to kilobytes.