GO - net/http - 如何获取请求字符串和方法

Mar*_*ace 6 go

使用net/http我想要一种获取当前请求字符串和方法的方法,即GET,POST,PUT等.

这是可能的 - 我无法在文档中看到它

And*_*sio 11

您可以使用Request结构的以下字段:

Method string

RequestURI string

我建议你看一下struct的源代码:Request struct source code

您可以通过单击net/http包的go doc中的结构名称来访问它.


lnm*_*nmx 7

在文档中,从http://golang.org/pkg/net/http开始,并按照"类型请求"链接访问http://golang.org/pkg/net/http#Request.您需要的一切都可以作为Request的字段或方法.

例如,HTTP方法是Request.Method.路径和查询参数分别位于Request.URL.Path和Request.URL.Query()中.

http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "%s %q", r.Method, html.EscapeString(r.URL.Path))
})

log.Fatal(http.ListenAndServe(":8080", nil))
Run Code Online (Sandbox Code Playgroud)