Go 为什么不是';t正在填充此HTTP请求的响应字段?
类型Go 为什么不是';t正在填充此HTTP请求的响应字段?,go,webserver,httprequest,httpresponse,http-redirect,Go,Webserver,Httprequest,Httpresponse,Http Redirect,类型http.Request中的Response字段的注释如下所示 // Response is the redirect response which caused this request // to be created. This field is only populated during client // redirects. Response *Response 然而,在我看来,这个字段在请求期间并没有被填充,因为这意味着它是被填充的。考虑下面的例子: package main
http.Request
中的Response
字段的注释如下所示
// Response is the redirect response which caused this request
// to be created. This field is only populated during client
// redirects.
Response *Response
然而,在我看来,这个字段在请求期间并没有被填充,因为这意味着它是被填充的。考虑下面的例子:
package main
import (
"net/http"
"log"
"fmt"
)
func handleA(writer http.ResponseWriter, request *http.Request) {
http.Redirect(writer, request, "/b", http.StatusSeeOther)
}
func handleB(writer http.ResponseWriter, request *http.Request) {
fmt.Println(request.Response)
}
func main() {
http.HandleFunc("/a", handleA)
http.HandleFunc("/b", handleB)
log.Fatal(http.ListenAndServe(":8080", nil))
}
如果我编译并运行此代码并导航到
localhost:8080/a
,那么我将被重定向到localhost:8080/b
,服务器将
打印到控制台。但它是否应该打印出一个非nil
值,因为请求是重定向的结果?在您的示例中,重定向是在浏览器中发生的;服务器无法知道是什么响应生成了重定向。该字段在从Go应用程序发出HTTP请求时填充;例如,如果使用http.Client
请求URL,并且响应是重定向,则它会为重定向URL生成一个新的request
,并且在该request
中,response
字段将填充触发该重定向的响应
http.Client:“此字段仅在客户端重定向期间填充。”这是服务器代码。