在GO中的http处理程序函数之间传递数据

时间:2016-03-25 07:09:42

标签: http go

我有一个http处理程序功能,只有在尚未收到该电子邮件时才会将电子邮件保存到数据库。如果电子邮件已被删除或其他一些数据无效,我想重定向回表单并通知用户错误发生的位置。

func validate(w http.ResponseWriter, r *http.Request) {

    //query database to check if data r.FormValues were
    //valid

    //IF form values were not valid redirect back to the
    //registration form and  pass on data indicating the
    //form data was not valid
    {
        http.Redirect(w, r, "/register", http.StatusFound)
    }

}

我如何将数据从func validate()发送到func寄存器()?是否可以在r * http.Request中添加某种结构,以便在调用http.Redirect()时将其传递给func register()?

1 个答案:

答案 0 :(得分:3)

在您的示例中,如果您的表单提交被定向到validate()处理程序,该处理程序将发回HTTP响应(这将是重定向),浏览器将再次调用/register。您的validate()处理程序与register()处理程序之间没有任何关联,*http.Request值与浏览器发出另一个HTTP请求的值不同,因此另一个*http.Request值将在第二次通话时创建并传递。

您可以在重定向网址中指定参数,例如重定向到/register?someParam=someValue,但这只是一次不必要的往返,并使事情复杂化。

更简单的解决方案是不分离表单呈现和验证(在处理程序级别)。相同的处理程序可以处理两者,因此两个处理程序之间不需要共享数据。

示例:

func register(w http.ResponseWriter, r *http.Request) {
    // Params for rendering the page
    m := map[string]interface{}{}

    // Is form submitted?
    if r.FormValue("submitRegister") != "" {
        // check submitted values
        // E.g. check email, let's say it's already in use:
        email := r.FormValue("Email")
        if alreadyInUse {
            m["Error"] = "Email already in use!"
        }

        if m["Error"] == "" {
            // If all values are OK, create user, and redirect:
            http.Redirect(w, r, "/home", http.StatusFound)
            return // AND return!
        }

        // Store submitted values in params, so when we render
        // the registration form again, we fill submitted params as initial values,
        // so user don't have to fill everything again (expect maybe the password)
        m["Email"] = email
    }

    // Either no submit or validation errors
    // Render the registration form, using submitted values as initial ones
    // Also if m["Error"] is set, render the error above the form
    registerTempl.Execute(w, m)
}

当然你可以将它分解为函数,你可以有一个单独的validate()函数,但是表单submit仍然必须指向与你的注册页面相同的路径(例如/register) :

func register(w http.ResponseWriter, r *http.Request) {
    // Params for rendering the page
    m := map[string]interface{}{}

    // Is form submitted?
    if r.FormValue("submitRegister") != "" {
        validate(w, r, m)
        if m["Error"] == "" {
            return // AND return!
        }
    }

    // Either no submit or validation errors
    // Render the registration form, using submitted values as initial ones
    // Also if m["Error"] is set, render the error above the form
    registerTempl.Execute(w, m)
}

func validate(w http.ResponseWriter, r *http.Request, m map[string]interface{}) {
    // check submitted values
    // E.g. check email, let's say it's already in use:
    email := r.FormValue("Email")
    if alreadyInUse {
        m["Error"] = "Email already in use!"
    }

    if m["Error"] == "" {
        // If all values are OK, create user, and redirect:
        http.Redirect(w, r, "/home", http.StatusFound)
        return
    }

    // Store submitted values in params, so when we
    // render the registration form again, we fill submitted params as initial values,
    // so user don't have to fill everything again (expect maybe the password)
    m["Email"] = email
}