使用Golang的HTML表单方法Post

使用Golang的HTML表单方法Post,html,forms,go,post,methods,Html,Forms,Go,Post,Methods,所以,我有一个html格式的表单。其目的是向/subscribe页面发出POST请求: <html> <form action="/subscribe" method="post"> First Name: <input type="text" name="first_name" placeholder="Willy"/><br/> Last Name: <input type="text" name="last_name

所以,我有一个html格式的表单。其目的是向
/subscribe
页面发出POST请求:

<html>
  <form action="/subscribe" method="post">
    First Name: <input type="text" name="first_name" placeholder="Willy"/><br/>
    Last Name: <input type="text" name="last_name" placeholder="Warmwood"/><br/>
    Email: <input type="email" name="email" placeholder="willy.warwood@gmail.com"/><br/>
    <input type="submit" value="Submit"/>
  </form>
</html>
而这名戈朗的处理人:

http.HandleFunc("/subscribe/", SubscribeHandler)
func SubscribeHandler(w http.ResponseWriter, r *http.Request) {
    log.Println(r.Method)
}
但问题是,它总是打印
GET

如何发布表单,因此
r.Method
的值是
post

根据文档,谢谢您:

如果已注册子树,并且收到一个请求,该请求命名子树根而不带尾随斜杠,则ServeMax会将该请求重定向到子树根(添加尾随斜杠)。可以使用不带尾随斜杠的路径的单独注册来覆盖此行为

由于您使用斜杠注册了
“/subscribe/”
,因此它被注册为子树。同样,根据文档:

以斜杠结尾的模式命名根子树

由于HTTP重定向(实际上)始终是GET请求,因此重定向后的方法当然是GET。您可以看到,在本例中发生了真正的重定向:

解决方案是注册以下两项:

http.HandleFunc("/subscribe/", SubscribeHandler)
http.HandleFunc("/subscribe", SubscribeHandler)
或者将您的表单指向带有
/
的表单:

<form action="/subscribe/" method="post">


是否确实正在为表单提交调用此处理程序?处理程序绑定到
/subscribe/
,但您的表单指向
/subscribe
。我一直在想为什么表单总是发送
get
请求。现在,我知道这是因为请求被重定向了。好的演示示例。谢谢