DFG
DFG

Reputation: 37

Problem redirecting and using gorilla sessions in golang

Hello everyone I ask for help because I have a problem when I want to redirect and save sessions with gorilla sessions, I have a url which validates an http post request, and this is the code:

if err != nil {
  // here i create a session using gorilla sessions with a function that i created
  flash := NewFlashSession("errors", c)

  // and here i set the error with a function that i also created
  flash.Set("general", "Error :(")
  // and i guess this is where the error starts
  flash.Save()

  http.Redirect(r.Writer, r.Request, "url", statusCode)
}

Actually I have a bit more complex model to explain it all in this question, but in summary what the "flash.Save()" function does is use the ".Save()" method of gorilla sessions, which receives an object "*http.Request" and "http.ResponseWriter".

And the variable "err" is an error caused by some error in validation, but when an error occurs and I want to redirect, I get the following error:

http: superfluous answer. WriteHeader call

And after doing some research, I realized that this was the cause of the problem, but. How can I save a session using Gorilla Sessions and then do a redirect? Or is there any other way to do it?

I hope you have understood me, if I do not explain myself well or if you need more information, I will try to show more of the code, I did not do it because it seems to me that the error is when I save the session and then do the redirection, and it seems to me needless to show the rest of the code. Thank you in advance :D.

Upvotes: 0

Views: 352

Answers (1)

DFG
DFG

Reputation: 37

I tried your code, and as they put in the comments, when adding a return there is no longer an error:

if err != nil {
  // here i create a session using gorilla sessions with a function that i created
  flash := NewFlashSession("errors", c)

  // and here i set the error with a function that i also created
  flash.Set("general", "Error :(")
  // and i guess this is where the error starts
  flash.Save()

  http.Redirect(r.Writer, r.Request, "url", statusCode)
  return;
}

Upvotes: 0

Related Questions