I am creating an endpoint and I am using the JSONContentExtractor middleware to obtain the context, but if I use it I cannot obtain the Body in the handler.
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
"go.mnc.gt/middleware"
)
type RequestBody struct {
Id int `json:"id"`
FirstName string `json:"first_name"`
}
func main() {
log.Println("starting API server")
//create a new router
router := mux.NewRouter()
log.Println("creating routes")
// Add external routes wthout encryption
middlewaresChainWithoutSessionAndWithoutEncryption := middleware.MiddlewaresChain(middleware.JSONContentExtractor)
//specify endpoints
router.HandleFunc("/personsMiddleware", middlewaresChainWithoutSessionAndWithoutEncryption(Persons)).Methods("POST")
http.Handle("/", router)
//start and listen to requests
http.ListenAndServe(":8080", router)
}
func Persons(w http.ResponseWriter, r *http.Request) {
log.Println("entering persons end point")
var requestBody RequestBody
err := json.NewDecoder(r.Body).Decode(&requestBody)
if err != nil {
http.Error(w, "Error al decodificar el cuerpo de la petición", http.StatusBadRequest)
return
}
fmt.Printf("Cuerpo de la petición: %+v\n", requestBody)
}
In the middleware it is possible to read the Body, and in the trace, it also appears, but I cannot take it from the Body again, I know it is possible because the middleware makes the body available again with the line r.Body = ioutil.NopCloser(bytes.NewBuffer(rawRequestBody))
This is the error I'm getting
2023/11/09 13:39:37 starting API server
2023/11/09 13:39:37 creating routes
2023-11-09T13:39:38.686-0600 DEBUG [email protected]/json_content_extractor_middleware.go:34 request body: {
"id": 1,
"first_name": "first_name",
"last_name": "last_name"
}
2023/11/09 13:39:38 entering persons end point
Cuerpo de la petición: {Id:0 FirstName: LastName:}
2023-11-09T13:39:38.686-0600 ERROR [email protected]/event_publisher.go:27
Event publisher not configured. Event may be lost
/Users/tribal/go/pkg/mod/go.mnc.gt/[email protected]/event_publisher.go:27 (go.mnc.gt/trace.publishEvent)
2023-11-09T13:39:38.687-0600 ERROR [email protected]/event_publisher.go:37 Unsent event: {"traceID":":1699558778686647000 /personsMiddleware","httpMethod":"POST","host":"localhost:8080","ipAddress":"[::1]:52249","path":"/personsMiddleware","startTime":1699558778,"endTime":1699558778,"elapsedTime":0,"appVersion":"","request":{"id":1,"first_name":"first_name","last_name":"last_name"},"response":null,"status":"Ok","device":{"uuid":"","os":"","osVersion":""}}
I am trying to get the body of the request. I have tried to obtain the body of the context (maybe not ideal, but I have not succeeded either). Without the middleware I can obtain the body of the request without problem.
I'm thinking it may be the contents of the config.toml file, but I'm not sure what the contents should be, it's currently a blank file.
Your first middleware is reading from the request body. You need to provide a way to re-read the request body. See https://pkg.go.dev/net/http#Request