在golang中重定向和使用gorilla sessions的问题

huangapple go评论82阅读模式
英文:

Problem redirecting and using gorilla sessions in golang

问题

大家好,我请求帮助,因为我在使用Gorilla Sessions重定向和保存会话时遇到了问题。我有一个验证HTTP POST请求的URL,以下是代码示例:

if err != nil {
  // 在这里,我使用自己创建的函数使用Gorilla Sessions创建一个会话
  flash := NewFlashSession("errors", c)

  // 在这里,我使用另一个自己创建的函数设置错误信息
  flash.Set("general", "Error :(")
  // 我猜这就是错误开始的地方
  flash.Save()

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

实际上,我有一个更复杂的模型来解释所有这些问题,但总结起来,"flash.Save()" 函数使用了 Gorilla Sessions 的 ".Save()" 方法,该方法接收一个 "*http.Request" 对象和 "http.ResponseWriter" 对象。

变量 "err" 是由验证过程中的某个错误引起的,但当出现错误并且我想要重定向时,我会得到以下错误信息:

http: superfluous answer. WriteHeader call

经过一些研究,我意识到这是问题的原因。但是,我应该如何使用 Gorilla Sessions 保存会话并进行重定向呢?或者是否有其他方法可以实现这个目的?

希望你能理解我的问题。如果我没有解释清楚或者你需要更多信息,我可以尝试展示更多代码。我之前没有这样做是因为我认为错误发生在保存会话后进行重定向时,我认为没有必要展示其他代码。提前谢谢你的帮助 :D。

英文:

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.

答案1

得分: 0

我尝试了你的代码,并且按照评论中的建议,在添加了return之后,错误就不再存在了:

if err != nil {
  // 这里我使用了一个我创建的函数,使用gorilla sessions创建了一个会话
  flash := NewFlashSession("errors", c)

  // 这里我使用了另一个我创建的函数,设置了错误信息
  flash.Set("general", "Error :(")
  // 我猜错误就是从这里开始的
  flash.Save()

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

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;
}

huangapple
  • 本文由 发表于 2022年8月23日 01:17:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/73448815.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定