我正在尝试从 html 响应中解码 XML。
=>我将此响应正文作为字符串保存到变量中,并使用 xml.Unmarshal 函数 .Code 成功解码:
err = xml.Unmarshal([]byte(outs), &v)
if err != nil {
fmt.Printf("error is here: %v", err)
return
}
Run Code Online (Sandbox Code Playgroud)
所以我认为问题不在于响应正文的实际内容。
现在我的实际代码:
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
Run Code Online (Sandbox Code Playgroud)
响应打印在上面代码的最后一行。但下面的代码给出“错误:EOF”
if err := xml.NewDecoder(resp1.Body).Decode(&v); err != nil {
fmt.Printf("error is : %v", err)
return
}
Run Code Online (Sandbox Code Playgroud)
我的代码有什么问题。请帮助
如果您已经阅读了Body io.ReadCloser一次(使用conts1, err := ioutil.ReadAll(resp1.Body)),则不能要求另一个函数再次阅读它(否则您将收到EOF错误消息)。
我将此响应正文作为字符串保存到变量中,并使用 xml.Unmarshal 函数成功解码。
这似乎是多次使用正文内容的最简单方法。