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

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

GO - net/http - how to get request string and method

问题

使用net/http,我想要一种获取当前请求字符串和方法(例如GET、POST、PUT等)的方法。

这个可能吗?我在文档中没有看到。

英文:

Using the net/http i want a way to get the current request string and the method i.e. GET, POST, PUT etc.

Is this possible - i cant see it in the docs

答案1

得分: 13

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

Method string

RequestURI string

我建议你查看结构体的源代码:Request结构体源代码

你可以通过在net/http包的go doc中点击结构体名称来访问它。

英文:

You can use the following fields of the Request struct:

Method string

and

RequestURI string

I suggest you to look at the source code of the struct: Request struct source code

You can access it by clicking on the struct name in the go doc for the net/http package.

答案2

得分: 8

在文档中,从http://golang.org/pkg/net/http开始,并跟随"type Request"链接到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))
英文:

In the docs, start from http://golang.org/pkg/net/http and follow the "type Request" link to http://golang.org/pkg/net/http#Request. Everything you need is available as a field or method of Request.

For example, the HTTP method is Request.Method. The path and query parameters are in Request.URL.Path and Request.URL.Query(), respectively.

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))

答案3

得分: 0

是的,你可以。所有的数据都可以在HttpRequest中获得。如果你查看Request.HttpMethod,你会看到方法(例如Get)。如果需要,你还可以访问头信息。根据你的操作,FilePath、Path和其他几个属性将提供已发布的数据。

英文:

Yes you can. all of that data is available inside of the HttpRequest. If you look in the Request.HttpMethod you will see the method (ie Get). You also have access to the header information if needed as well. depending on what you are doing, the FilePath, Path and several other properties will provide you with the data that has been posted.

答案4

得分: 0

你可以使用r.Method来获取请求的相关信息(以字符串形式表示)(其他信息请参考https://golang.org/pkg/net/http/#Request)。

你可以使用r.URL.Query()来获取请求URL的相关信息(以map形式表示查询参数),使用r.URL.Path来获取请求URL的路径(以字符串形式表示)(其他信息请参考https://golang.org/pkg/net/url/#URL)。

如果你需要任何特定的帮助,请随时发送消息。

英文:

Request Related Info you can get it using: r.Method [to get method as string] (for others https://golang.org/pkg/net/http/#Request)

Request URL Related Info you can get it using: r.URL.Query() [to get query params as map], r.URL.Path [to get path as string] (for others check https://golang.org/pkg/net/url/#URL)

Feel free to msg if you need anything specific.

huangapple
  • 本文由 发表于 2013年7月30日 06:12:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/17935121.html
匿名

发表评论

匿名网友

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

确定