英文:
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, "Hello")
}
main function:
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/requests/", requestHandler)
http.ListenAndServe(":8000", mux)
}
Curl gives the following output:
>> 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:
&{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>}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论