Home > Article > Backend Development > How to Fix \"EOF Error\" When Decoding XML from HTML Response Body in Golang?
Error: xml.NewDecoder(resp.Body).Decode Giving EOF Error in Golang
When attempting to decode XML from an HTML response body, you may encounter an EOF error when using xml.NewDecoder(resp.Body).Decode(&v). This occurs when the body content has already been read once.
Solution
The key to solving this issue lies in understanding the nature of the body content. In the provided code, the body content is first read and stored in a string variable usingioutil.ReadAll(resp1.Body). However, once the content has been read, it cannot be read again by another function, such as xml.NewDecoder(resp1.Body).Decode(&v). This results in the EOF error.
The most convenient solution is to use the body content multiple times. One way to achieve this is through the xml.Unmarshal function, which allows you to decode XML from an array of bytes. Therefore, instead of using xml.NewDecoder(resp1.Body).Decode(&v), you can use the following code:
<code class="go">err = xml.Unmarshal([]byte(conts1), &v) if err != nil { fmt.Printf("error is here: %v", err) return }</code>
This approach allows you to read and decode the body content multiple times without encountering the EOF error.
The above is the detailed content of How to Fix \"EOF Error\" When Decoding XML from HTML Response Body in Golang?. For more information, please follow other related articles on the PHP Chinese website!