作为http客户端开始工作

作为http客户端开始工作,http,go,httpclient,Http,Go,Httpclient,我试图从json api获得响应,但我得到的是作为主体的。我不知道怎么了。下面是代码片段 client := &http.Client{} req, err := http.NewRequest("POST", "http://example.com", bytes.NewBufferString("{\"a\": \"b\"}")) resp, err := client.Do(req) defer resp.Body.Close() body, err := ioutil.Re

我试图从json api获得响应,但我得到的是作为主体的
。我不知道怎么了。下面是代码片段

client := &http.Client{}
req, err := http.NewRequest("POST", "http://example.com", 
  bytes.NewBufferString("{\"a\": \"b\"}"))
resp, err := client.Do(req)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
var f interface{}
err = json.Unmarshal(body, &f)
fmt.Println(f);

非常感谢您的帮助

请养成检查
err
是否为非零的习惯,如
如果出错!=零{panic(err)}
。它将为您节省大量时间。@kavu它就在那里,只是从这一个省略了。请记住,这只是代码片段。请说明如何检查错误。如果您得到的是一个nil响应,则表示您缺少某个错误检查。您还应该检查resp.StatusCode。如果您的帖子成功检索到404消息,err将为零。@ducktyped您好,我注意到这个问题仍然在未回答的队列中。请您提供一个答案并批准它……:)