I am pretty new to golang and trying to set my response headers. I have two headers that I want to set. I think that I am misunderstanding something fundamental. I am also using go-swagger to generate my endpoints.
My problem is that I can only seem to set one of my two headers. Swagger provides a function "auth.NewAuthLoginUserOK().WithProfileHeader("pickles)" on the return (in the "if success" block). How can I set two header params?
func AuthLoginRouteHandler(params auth.AuthLoginUserParams) middleware.Responder {
transactionId := redFalconLogger.GetTransactionId()
redFalconLogger.LogDebug("AuthLoginRouteHandler", transactionId)
email := params.Body.Email
password := params.Body.Password
//Check to ensure that they are not nil
if email == "" || password == ""{
redFalconLogger.LogError("Got an empty string on a username/password", transactionId)
return auth.NewAuthLoginUserBadRequest()
}
//use pointers to limit in flight private data
pointerEmail := &email
pointerPassword := &password
//Call the auth domain
success := authDomain.LoginUser(pointerEmail,pointerPassword,transactionId)
if success {
return auth.NewAuthLoginUserOK().WithProfileKeyHeader("pickles")
}
redFalconLogger.LogDebug("Failed Login: ", transactionId)
return auth.NewAuthLoginUserBadRequest()
}
Thank you in advance.
go-swagger will generate one method per response header defined in the spec on result objects (what's returned by auth.NewAuthLoginUserOK()
)
If you have multiple response headers defined in the spec you generated off of, just chain the calls.
return auth.NewAuthLoginUserOK().WithProfileKeyHeader("pickles").WithOtherHeader("cucumbers")
You should try and avoid deviating from the specification as much as possible. If you absolutely need to write a header not specified in the spec, the response object will have a ServeHTTP method you can use to get at the stdlib's ResponseWriter.
return auth.NewAuthLoginUserOK().ServeHTTP(func(rw http.ResponseWriter, r *http.Request) {
// Try and avoid this
rw.Header().Add("profile", "pickles")
rw.Header().Add("other-header", "cucumbers")
})