I have 2 middlewares and the final HandleFunc.
But the Request.Body only works in the first Middleware. When the 2nd starts, the Body is Empty.
What could be happening?
This is my call to the route.
router.HandleFunc("/registro", middleW.Validaciones(middleW.UsuarioYaExiste(routes.Registro))).Methods("POST")
and the first Middleware is
func Validaciones (next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if bd.ChequeoConnection()==0 {
http.Error(w,"Conexión Perdida con la Base de Datos",500)
return
}
var t models.Usuario
err := json.NewDecoder(r.Body).Decode(&t)
http.Error(w,"Nombre "+t.Nombre+" - Email "+t.Email+" - Password "+t.Password, 400)
if err != nil {
mensaje:="Validaciones : Usuario y/o Contraseña inválidos <br> Password ="+t.Password+"<br>Email = "+t.Email+"<br>Nombre = "+t.Nombre
http.Error(w, mensaje, 400)
return
}
if len(t.Email)==0 {
http.Error(w, "El email de usuario es requerido", 400)
return
}
if len(t.Password)<6 {
http.Error(w, "Debe especificar una contraseña de al menos 6 caracteres", 400)
return
}
/* si todo estuvo OK devuelve la función de la Ruta */
next.ServeHTTP(w, r)
}
}
this is the second Middleware
func UsuarioYaExiste (next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var t models.Usuario
err := json.NewDecoder(r.Body).Decode(&t)
http.Error(w,"Nombre 1ro. "+t.Nombre+" - Email "+t.Email+" - Password "+t.Password, 400)
if err != nil {
mensaje:="UsuarioYaExiste : Usuario y/o Contraseña inválidos <br> Password ="+t.Password+"<br>Email = "+t.Email+"<br>Nombre = "+t.Nombre
http.Error(w, mensaje, 400)
return
}
existe := bd.ChequeoYaExisteUsuario(t.Email)
if existe == true && r.Method == "POST" {
http.Error(w, "ya existe un usuario con ese email", 400)
return
}
if existe == false && r.Method == "GET" {
http.Error(w, "Usuario no existe", 400)
return
}
/* si todo estuvo OK devuelve la función de la Ruta */
next.ServeHTTP(w, r)
}
}
In this 2nd Middleware, this line
http.Error(w,"Nombre 1ro. "+t.Nombre+" - Email "+t.Email+" - Password "+t.Password, 400)
show empty values, but not in the first Middleware.