golang无效字符'b'寻找值的开始 [英] golang invalid character 'b' looking for beginning of value

查看:89
本文介绍了golang无效字符'b'寻找值的开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在其中发布带有xml消息的json.但是它返回

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

无效的字符"b"正在寻找值的开头

invalid character 'b' looking for beginning of value

我认为可能的原因是我试图封送非json格式的return正文.

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\n", req.Method, req.URL.Host, req.URL.Path)
    start := time.Now()

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

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

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

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

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

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

    return nil
}

此行发生错误

return json.Unmarshal(resBody, v)

推荐答案

该错误表示服务器未返回有效的JSON响应.我建议添加以下代码来调试问题:

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
}

这篇关于golang无效字符'b'寻找值的开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆