如何在http.Request中获取URL

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

How to get URL in http.Request

问题

我构建了一个HTTP服务器。我正在使用下面的代码来获取请求的URL,但它无法获取完整的URL。

func Handler(w http.ResponseWriter, r *http.Request) {
    fmt.Printf("Req: %s %s", r.URL.Host, r.URL.Path)
}

我只得到了"Req: / ""Req: /favicon.ico"

我想要获取完整的客户端请求URL,如"1.2.3.4/""1.2.3.4/favicon.ico"

谢谢。

英文:

I built an HTTP server. I am using the code below to get the request URL, but it does not get full URL.

func Handler(w http.ResponseWriter, r *http.Request) {  
    fmt.Printf("Req: %s %s", r.URL.Host, r.URL.Path)
}

I only get "Req: / " and "Req: /favicon.ico".

I want to get full client request URL as "1.2.3.4/" or "1.2.3.4/favicon.ico".

Thanks.

答案1

得分: 67

根据net/http包的文档:

type Request struct {
   ...
   // 请求的主机名。
   // 根据RFC 2616,它可以是Host:头部的值,也可以是URL本身中给出的主机名。
   // 它的格式可以是"host:port"。
   Host string
   ...
}

修改后的代码版本:

func Handler(w http.ResponseWriter, r *http.Request) {
	fmt.Printf("Req: %s %s\n", r.Host, r.URL.Path) 
}

示例输出:

Req: localhost:8888 /
英文:

From the documentation of net/http package:

type Request struct {
   ...
   // The host on which the URL is sought.
   // Per RFC 2616, this is either the value of the Host: header
   // or the host name given in the URL itself.
   // It may be of the form "host:port".
   Host string
   ...
}

Modified version of your code:

func Handler(w http.ResponseWriter, r *http.Request) {
	fmt.Printf("Req: %s %s\n", r.Host, r.URL.Path) 
}

Example output:

Req: localhost:8888 /

答案2

得分: 19

我使用req.URL.RequestURI()来获取完整的URL。

net/http/requests.go文件中:

// RequestURI是客户端发送给服务器的请求行(RFC 2616,第5.1节)中未修改的Request-URI。
// 通常应该使用URL字段代替它。
// 在HTTP客户端请求中设置此字段是错误的。
RequestURI string

英文:

I use req.URL.RequestURI() to get the full url.

From net/http/requests.go :

// RequestURI is the unmodified Request-URI of the
// Request-Line (RFC 2616, Section 5.1) as sent by the client
// to a server. Usually the URL field should be used instead.
// It is an error to set this field in an HTTP client request.
RequestURI string

答案3

得分: 9

如果你检测到正在处理一个相对URL(r.URL.IsAbs() == false),你仍然可以访问r.Host(参见http.Request),即Host本身。

将这两个连接起来就可以得到完整的URL。

通常情况下,你会看到相反的情况(从URL中提取Host),就像在gorilla/reverse/matchers.go中所示:

// getHost尽力返回请求的主机。
func getHost(r *http.Request) string {
    if r.URL.IsAbs() {
        host := r.Host
        // 切掉任何端口信息。
        if i := strings.Index(host, ":"); i != -1 {
            host = host[:i]
        }
        return host
    }
    return r.URL.Host
}
英文:

If you detect that you are dealing with a relative URL (r.URL.IsAbs() == false), you sill have access to r.Host (see http.Request), the Host itself.

Concatenating the two would give you the full URL.

Generally, you see the reverse (extracting Host from an URL), as in gorilla/reverse/matchers.go

<!-- language-all: go -->

// getHost tries its best to return the request host.
func getHost(r *http.Request) string {
	if r.URL.IsAbs() {
		host := r.Host
		// Slice off any port information.
		if i := strings.Index(host, &quot;:&quot;); i != -1 {
			host = host[:i]
		}
		return host
	}
	return r.URL.Host
}

huangapple
  • 本文由 发表于 2014年4月18日 18:04:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/23151827.html
匿名

发表评论

匿名网友

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

确定