error: EOF當我閱讀 XML 響應正文時,我正在進入控制臺。下面是我的代碼。resp, err := http.Post(url, "application/xml", payload)if err != nil { response.WriteErrorString(http.StatusInternalServerError, err.Error()) return}defer resp.Body.Close()dec := xml.NewDecoder(resp.Body)if debug == true { body, err := ioutil.ReadAll(resp.Body) fmt.Println("=========== Response ==================") if err != nil { fmt.Printf("error: %v", err) return } fmt.Println(string(body)) fmt.Println("=========== Response Ends =============")}err = dec.Decode(respStruct)我懷疑ioutil.ReadAll沒有按預期工作。是否有原因引發此錯誤?
1 回答

慕虎7371278
TA貢獻1802條經驗 獲得超4個贊
xml.NewDecoder(resp.Body)
可能已經閱讀了 resp.Body 的內容。
因此EOF
消息。
你可以在“ xml.NewDecoder(resp.Body).Decode
Giving EOF
Error ”中看到同樣的錯誤
讀取第resp.Body
一個,并使用字符串 withxml.Unmarshal
將避免雙重讀取和錯誤消息。
注意:類似的答案表明最佳實踐仍然是使用xml.Decoder
而不是xml.Unmarshal
從流中讀取。
因此,請確保您不要閱讀resp.Body
兩次,它會起作用。
- 1 回答
- 0 關注
- 561 瀏覽
添加回答
舉報
0/150
提交
取消