xml.NewDecoder(resp.Body)给出EOF错误_GOLang的解码

【字号: 日期:2024-03-10浏览:45作者:雯心
(adsbygoogle = window.adsbygoogle || []).push({}); 如何解决xml.NewDecoder(resp.Body)给出EOF错误_GOLang的解码?

如果您已经读过Bodyio.ReadCloser一次(带有conts1, err :=IoUtil.ReadAll(resp1.Body)),则不能要求另一个函数再次读取它(否则您将收到EOF错误消息)。

我将此响应主体作为字符串保存到变量中,并使用xml.Unmarshal函数成功解码。

这似乎是多次使用身体内容的最简单方法。

解决方法

我正在尝试从HTML响应正文中解码XML。

=>我将此响应主体作为字符串保存到变量中,并使用xml.Unmarshal Function .Code成功解码:

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 lineresp1,err := client.Do(req1)error_handler(err)fmt.Printf('n %s n',resp1.Status)defer resp1.Body.Close()//I tried with and without this lineconts1,err := ioutil.ReadAll(resp1.Body)error_handler(err)fmt.Println('Response Body is Here :',string(conts1))//Contents are Printed Here

响应打印在上面的代码的最后一行。但是下面的代码给出了“ Error:EOF”

if err := xml.NewDecoder(resp1.Body).Decode(&v); err != nil { fmt.Printf('error is : %v',err) return}

我的代码有什么问题。请帮助

相关文章: