xml.NewDecoder(resp.Body).Decode 给出 EOF 错误 _GOLang
时间:2023-03-27
浏览:357
作者:佚名
大家好,今天本人给大家带来文章《xml.NewDecoder(resp.Body).Decode 给出 EOF 错误 _GOLang》,文中内容主要涉及到golang,如果你对Golang方面的知识点感兴趣,那就请各位朋友继续看下去吧~希望能真正帮到你们,谢谢!
问题内容
我正在尝试从正文中解码 XML 的 html 响应。
=>我将此响应正文作为字符串保存到变量中,并使用 xml.Unmarshal 函数成功解码。代码:
err = xml.Unmarshal([]byte(outs), &v)
if err != nil {
fmt.Printf("error is here: %v", err)
return
}
所以我认为问题不在于响应正文的实际内容。
现在我的实际代码:
req1, err := http.NewRequest("GET", concat([]string{domain, defects_link}), nil)
error_handler(err)
req1.Close = true //I tried with and without this line
resp1, err := client.Do(req1)
error_handler(err)
fmt.Printf("\n %s \n", resp1.Status)
defer resp1.Body.Close()//I tried with and without this line
conts1, err := ioutil.ReadAll(resp1.Body)
error_handler(err)
fmt.Println("Response Body is Here :", string(conts1))//Contents are Printed Here
响应打印在上面代码的最后一行。但下面的代码给出“错误:EOF”
if err := xml.NewDecoder(resp1.Body).Decode(&v); err != nil {
fmt.Printf("error is : %v", err)
return
}
我的代码有什么问题。请帮助
正确答案
如果您已经阅读过Body io.ReadCloser一次(使用conts1, err := ioutil.ReadAll(resp1.Body)),则不能要求另一个函数再次阅读(否则您将收到EOF错误消息)。
我将此响应正文作为字符串保存到变量中,并使用 xml.Unmarshal 函数成功解码。
这似乎是多次使用正文内容的最简单方法。
到这里,我们也就讲完了《xml.NewDecoder(resp.Body).Decode 给出 EOF 错误 _GOLang》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于golang的知识点!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
12.01
12.01
热门专题
