Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 为什么不是';t正在填充此HTTP请求的响应字段?_Go_Webserver_Httprequest_Httpresponse_Http Redirect - Fatal编程技术网

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:

“此字段仅在客户端重定向期间填充。”这是服务器代码。