Access HTTP response as string in Go

后端 未结 3 1326
鱼传尺愫
鱼传尺愫 2020-12-12 13:16

I\'d like to parse the response of a web request, but I\'m getting trouble accessing it as string.

func main() {
    resp, err := http.Get(\"http://google.h         


        
3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-12 13:58

    The method you're using to read the http body response returns a byte slice:

    func ReadAll(r io.Reader) ([]byte, error)
    

    official documentation

    You can convert []byte to a string by using

    body, err := ioutil.ReadAll(resp.Body)
    bodyString := string(body)
    

提交回复
热议问题