Golang http: respuesta múltiple. Llamadas de WriteHeader
Por lo tanto, actualmente estoy escribiendo un inicio de sesión y, respectivamente, una función de registro para mi aplicación web Go y estoy intentando implementar una función que, si no completa los campos de formulario requeridos "nombre de usuario" "contraseña", le dará unhttp.Error
y luego intento hacerlohttp.Redirect
Sin embargo, aparece este error cuando se produce la redirección.http: multiple response.WriteHeader calls
Aquí está mi código ...
//Check form submission
var u user
if req.Method == http.MethodPost {
un := req.FormValue("username")
p := req.FormValue("password")
//Checking to see if user filled out required fields.
if un == ""{
http.Error(w, "Please fill out required fields, you will be redirected shortly.", http.StatusForbidden)
time.Sleep(3000 * time.Millisecond)
//http.Redirect(w, req, "/" http.StatusSeeOther)
return
}else if p == "" {
http.Error(w, "Please fill out required fields, you will be redirected shortly.", http.StatusForbidden)
time.Sleep(3000 * time.Millisecond)
//http.Redirect(w, req, "/", http.StatusSeeOther)
return
}
c.Value = un
u = user{un, p}
dbUsers[c.Value] = u
http.Redirect(w, req, "/login", http.StatusSeeOther)
log.Println(dbUsers)
return
}
Sé que es debido a las múltiples llamadas http dentro de la instrucción if / else, pero no puedo encontrar una alternativa. Cualquier ayuda sería muy apreciada!