普通网友 2016-08-14 14:14
浏览 169
已采纳

golang无效字符'b'寻找值的开始

I am trying to post a json with xml message inside it. However it returns

invalid character 'b' looking for beginning of value

I think the possible reason is that I am trying to marshal the returns body which is not in json format.

func (s *BackendConfiguration) Do(req *http.Request, v interface{}) error {
    log.Printf("Requesting %v %v%v
", req.Method, req.URL.Host, req.URL.Path)
    start := time.Now()

    res, err := s.HTTPClient.Do(req)

    if debug {
        log.Printf("Completed in %v
", time.Since(start))
    }

    if err != nil {
        log.Printf("Request to sakura failed: %v
", err)
        return err
    }
    defer res.Body.Close()

    resBody, err := ioutil.ReadAll(res.Body)
    if err != nil {
        log.Printf("Cannot parse sakura response: %v
", err)
        return err
    }

    if debug {
        log.Printf("sakura response: %q
", resBody)
    }

    if v != nil {
        return json.Unmarshal(resBody, v)
    }

    return nil
}

The error happened at this line

return json.Unmarshal(resBody, v)
  • 写回答

1条回答 默认 最新

  • dpwdsmbvm496180204 2016-08-15 14:29
    关注

    The error indicates that the server did not return a valid JSON response. I suggest adding the following code to debug the issue:

    err := json.Unmarshal(resBody, v)
    if err != nil {
        log.Printf("error decoding sakura response: %v", err)
        if e, ok := err.(*json.SyntaxError); ok {
            log.Printf("syntax error at byte offset %d", e.Offset)
        }
        log.Printf("sakura response: %q", resBody)
        return err
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度