Golang-DumpRequest()未为ReadRequest()创建正确的输出?

Golang-DumpRequest()未为ReadRequest()创建正确的输出?,go,Go,创建http请求的字节数组,然后尝试将其读入http.request,但当请求包含正文时,该请求似乎不起作用 req, _ := http.NewRequest(http.MethodPost, "/Bar", strings.NewReader("Foo")) rReq, _ := httputil.DumpRequest(req, true) req2, _ := http.ReadRequest(bufio.NewReader(bytes.NewReader(rReq))) b, _ :

创建http请求的字节数组,然后尝试将其读入http.request,但当请求包含正文时,该请求似乎不起作用

req, _ := http.NewRequest(http.MethodPost, "/Bar", strings.NewReader("Foo"))
rReq, _ := httputil.DumpRequest(req, true)

req2, _ := http.ReadRequest(bufio.NewReader(bytes.NewReader(rReq)))
b, _ := ioutil.ReadAll(req2.Body)
fmt.Println(b)

b是一个空数组。

代码中有两处错误:

  • 你必须处理这些错误。这将有助于您看到您从未构造有效的请求(“/Bar”不是有效的URL)

  • 对传出请求使用
    httputil.DumpRequestOut


  • 要点:始终处理所有错误并始终阅读整个文件包。

    代码中有两个错误:

  • 你必须处理这些错误。这将有助于您看到您从未构造有效的请求(“/Bar”不是有效的URL)

  • 对传出请求使用
    httputil.DumpRequestOut


  • 要点:始终处理所有错误并始终阅读整个
    文件包。

    没有办法对“无效”URL使用这些方法吗?详细解释购买mt URL必须是“/Foo/Bar”格式。我刚刚用err和
    if err!=nil{fmt.Println(err)}
    并且没有一个返回错误。对无效URL的HTTP请求怎么可能成功?URL中没有主机时要联系哪个主机?所以没有。
    http.NewRequest(http.MethodPost,“/Bar”,strings.NewReader(“Foo”))
    失败。没有办法对“无效”URL使用这些方法吗?详细解释购买mt URL必须是“/Foo/Bar”格式。我刚刚用err和
    if err!=nil{fmt.Println(err)}
    并且没有一个返回错误。对无效URL的HTTP请求怎么可能成功?URL中没有主机时要联系哪个主机?因此,No.
    http.NewRequest(http.MethodPost,“/Bar”,strings.NewReader(“Foo”))
    失败。