drtppp75155 2014-07-22 05:29
浏览 256
已采纳

xml.NewDecoder(resp.Body)。给出EOF错误_GOLang的解码

I'm trying to Decode XML from body a html response.

=>I saved this response body as string to a Variable and successfully decoded Using xml.Unmarshal Function .Code for that :

    err = xml.Unmarshal([]byte(outs), &v)
       if err != nil {
    fmt.Printf("error is here: %v", err)
    return
                       }

So I think problem is NOT with actual Content of Response body.

Now My Actual Code :

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("
 %s 
", 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

Response is printed in last line of above code.But below code is giving "Error :EOF"

    if err := xml.NewDecoder(resp1.Body).Decode(&v); err != nil {
    fmt.Printf("error is : %v", err)

    return
}

What is wrong in my code.Kindly Help

  • 写回答

1条回答 默认 最新

  • douxian1939 2014-07-22 06:11
    关注

    If you already read the Body io.ReadCloser once (with conts1, err := ioutil.ReadAll(resp1.Body)), you cannot ask another function to read it again (or you will get the EOF error message).

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

    That seems the easiest approach to use the body content multiple times.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来