doubi12138 2015-12-03 05:30
浏览 24
已采纳

前往:XML API返回奇数编码的字符串

I'm trying to parse the XML response from an API, and when call fmt.Println and pass the response body, I get a weird string:

&{0xc8200e6140 {0 0} false <nil> 0xc2030 0xc1fd0}

I've confirmed that I can curl the API and get XML as expected. (I also get the same response sending a GET request with the Postman Chrome extension.) Is this an encoding issue?

Here's the relevant code:

type Album struct {
    Title     string `xml:"album>name"`
    Artist    string `xml:"album>artist>name"`
    PlayCount uint64 `xml:"album>playcount"`
}

const lastFMAPIKey string = "<My api key>"
const APIURL string = "http://ws.audioscrobbler.com/2.0/"

func perror(err error) {
    if err != nil {
        panic(err)
    }
}

func getListeningInfo(url string) []byte {
    resp, err := http.Get(url)
    perror(err)
    defer resp.Body.Close()
    // this is the line that prints the string above
    fmt.Println(resp.Body)
    body, err2 := ioutil.ReadAll(resp.Body)
    perror(err2)
    return body
}

func main() {
    url := APIURL + "?method=user.getTopAlbums&user=iamnicholascox&period=1month&limit=1&api_key=" + lastFMAPIKey
    album := Album{}
    err := xml.Unmarshal(getListeningInfo(url), &album)
    perror(err)
    fmt.Printf(album.Artist)
}

For reference, printing out resp instead of just resp.Body gives this:

{200 OK 200 HTTP/1.1 1 1 map[Ntcoent-Length:[871]
Connection:[keep-alive] Access-Control-Max-Age:[86400]
Cache-Control:[private] Date:[Thu, 03 Dec 2015 05:16:34 GMT]
Content-Type:[text/xml; charset=UTF-8]
Access-Control-Request-Headers:[Origin, X-Atmosphere-tracking-id, X-Atmosphere-Framework, X-Cache-Date,
Content-Type, X-Atmosphere-Transport, *]
Access-Control-Allow-Methods:[POST, GET, OPTIONS]
Access-Control-Allow-Origin:[*]
Server:[openresty/1.7.7.2]]
0xc8200f6040 -1 [] false map[] 0xc8200b8000 <nil>}
  • 写回答

1条回答 默认 最新

  • dongzha5934 2015-12-03 06:06
    关注

    The http.Response's Body is an io.ReaderCloser. The odd output you are seeing is the values of the fields of struct used as the response body.

    if you want the actual content to print out, you must read it from the Body first.

    Try ioutil.ReadAll by doing:

    b, err := ioutil.ReadAll(resp.Body) // b is a []byte here
    if err != nil { 
       fmt.Println("Got error:",err)
    } else {
        fmt.Println(string(b)) // convert []byte to string for printing to the screen.
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入