Go Web服务器无法处理POST请求

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

Go Webserver Not Handling POST Requests

问题

我正在使用Go的http包创建一个简单的Web服务器。我只注册了一个处理程序,用于处理路径为"/requests/"的请求。

它可以很好地处理GET请求,但是当我发送POST请求时,处理程序从未被调用,客户端会收到301 Moved Permanently的响应。

我尝试搜索了一下,但似乎这不是人们常遇到的问题。

我的处理程序如下:

func requestHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello")
}

主函数如下:

func main() {
    mux := http.NewServeMux()
    mux.HandleFunc("/requests/", requestHandler)
    http.ListenAndServe(":8000", mux)
}

使用Curl命令会得到以下输出:

>> curl -i -X POST http://localhost:8000/requests
HTTP/1.1 301 Moved Permanently
Location: /requests/
Date: Thu, 12 Jan 2017 08:51:10 GMT
Content-Length: 0
Content-Type: text/plain; charset=utf-8

Go自带的http客户端返回类似的响应对象:

 &{301 Moved Permanently 301 HTTP/1.1 1 1 map[Content-Type:[text/plain; charset=utf-8] Location:[/requests/] Date:[Thu, 12 Jan 2017 08:51:58 GMT] Content-Length:[0]] 0x339190 0 [] false false map[] 0xc4200cc0f0 <nil>}

再次强调,GET请求的行为与我预期的一样,并调用了处理程序函数。我需要做一些不同的事情来处理POST请求吗?谢谢您的帮助!

英文:

I'm creating a simple web server using Go's http package. I'm registering just one handler, for requests to the path "/requests/".

It can handle GET requests just fine but when I send a POST request, the handler is never invoked and the client gets a 301 Moved Permanently response.

I've tried searching for this but it appears this isn't a problem people are commonly facing.

My handler is:

func requestHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, &quot;Hello&quot;)
}

main function:

func main() {
    mux := http.NewServeMux()
    mux.HandleFunc(&quot;/requests/&quot;, requestHandler)
    http.ListenAndServe(&quot;:8000&quot;, mux)
}

Curl gives the following output:

&gt;&gt; curl -i -X POST http://localhost:8000/requests
HTTP/1.1 301 Moved Permanently
Location: /requests/
Date: Thu, 12 Jan 2017 08:51:10 GMT
Content-Length: 0
Content-Type: text/plain; charset=utf-8

Go's own http client returns a similar response object:

 &amp;{301 Moved Permanently 301 HTTP/1.1 1 1 map[Content-Type:[text/plain; charset=utf-8] Location:[/requests/] Date:[Thu, 12 Jan 2017 08:51:58 GMT] Content-Length:[0]] 0x339190 0 [] false false map[] 0xc4200cc0f0 &lt;nil&gt;}

Again, GET requests behave just as I'd expect them to and invoke the handler function. Do I need to do something differently to handle POST requests? Thanks for any help on this!

答案1

得分: 3

您正在查询/requests

重定向指向/requests/

您使用的curl命令如下:

curl localhost:8000/requests

您需要在mux.HandleFunc中使用/requests而不是/requests/

或者使用

curl localhost:8000/requests/

还请注意,如果您的请求在浏览器上可以正常工作而无需任何更改,那是因为浏览器会自动处理重定向。

如果在mux.HandleFunc中的路由没有尾部斜杠,带有尾部斜杠的路由将返回404错误。

PS:您的requestHandler处理所有方法,而不仅仅是POST请求。您需要检查r.Method以便根据不同的方法进行处理。

英文:

You are querying for /requests.

The redirect is pointing you to /requests/

You used curl like this :

curl localhost:8000/requests

You need to either use /requests instead of /requests/ in mux.HandleFunc.

or use

curl localhost:8000/requests/

Also note that if you request would work on a browser without any change as it handles redirects automatically.

And the route with trailing slash would return 404, if the route in mux.HandleFunc does not have a trailing slash.

PS : Your requestHandler handles all methods, not just POST requests. You need to check for r.Method to handle the methods differently.

huangapple
  • 本文由 发表于 2017年1月12日 17:13:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/41609104.html
匿名

发表评论

匿名网友

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

确定