dousha1873 2017-01-09 18:22
浏览 14
已采纳

如何在GO的请求中获得身体反应?

I'm trying to make a webserver that will work like a proxy. My server make requests (POST, GET ...) for the client to an specific site, receive the response from this site and give it to the client. As i said, like a proxy. The problem is: in the response from google.com for example (or any other site) i can't read the body. The status code is 200 but when i tried to read the body content i receive weird things.

This is the part of my code that i make the request

request, err := http.NewRequest(method, url, nil)
for k, v := range m {
    request.Header.Set(k, v)
}
if err != nil {
    log.Fatalln(err.Error())
}

client := http.Client{}
resp, err := client.Do(request)

if err != nil {
    log.Fatalln(err.Error())
}else{

fmt.Println("=======================")
fmt.Println(resp)
fmt.Println("=======================")
fmt.Println(resp.Body)

And i receive this:

=======================
&{200 OK 200 HTTP/1.1 1 1 map[Date:[Mon, 09 Jan 2017 18:07:49 GMT]
Cache-Control:[private, max-age=0] Content-Type:[text/html; 
charset=ISO-8859-1] P3p:[CP="This is not a P3P policy! See 
https://www.google.com/support/accounts/answer/151657?hl=en for more 
info."] Server:[gws] X-Xss-Protection:[1; mode=block] Expires:[-1] X-
Frame-Options:[SAMEORIGIN] Set-Cookie:[NID=94=i5qZWuqYtrLAkc-amGHbmDnqx3Wg8mGx0kuk6s-
gKWYMSNXbScl0Cb5GldDzGdfrIrJvHC3151JzHB2s3XLdmFN82-
_gSxu07xwPNbVlzKiZgE9dJf7vXeXSaYQhWowv; expires=Tue, 11-Jul-2017 
18:07:49 GMT; path=/; domain=.google.com.br; HttpOnly]] 0xc4200cac20 
-1 [] false true map[] 0xc420126000 <nil>}
=======================
&{0xc420014700 <nil> <nil>}
  • 写回答

1条回答 默认 最新

  • doutan3040 2017-01-09 18:25
    关注

    From the documentation https://golang.org/pkg/net/http/ to read the body response you can use the package ioutil

    Like :

     resp, err := http.Get("http://example.com/")
        if err != nil {
            // handle error
        }
        defer resp.Body.Close()
        body, err := ioutil.ReadAll(resp.Body)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度