html - xml.NewDecoder(resp.Body).Decode给出EOF错误_GOLang

标签 html xml go

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

=>我将这个响应主体作为字符串保存到一个变量中,并使用 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 错误信息).

I saved this response body as string to a Variable and successfully decoded Using xml.Unmarshal Function.

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

关于html - xml.NewDecoder(resp.Body).Decode给出EOF错误_GOLang,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24879587/

相关文章:

javascript - 带有 css3 keydown keyCode = 37 和 39 的 JQuery

javascript - 接收到服务信息后动态建表

javascript - 如何在遵循 <Href> 之前执行 Knockout.js 操作?

html - 如何用 HTML/CSS 获得这种文字环绕效果?

c# - 在 C# 中用 ""替换新行

java - 如何使用 java dom 从 xml 中删除 namespace ?

android - editText 和软键盘之间的空间

mongodb - 带有 golang mongo-driver 的多个 maxPoolSize 配置

date - golang 中过去日期时间和当前时间之间的差异(以分钟为单位)

go - REST HTTPS 服务器错误