我正在尝试从 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/