简体   繁体   English

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

[英]Golang invalid character 'b' looking for beginning of value

I am trying to post json with a xml message inside it.我正在尝试在其中发布带有 xml 消息的 json。 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 the json format.我认为可能的原因是我试图编组不是 json 格式的返回主体。

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
}

The error happened at this line:错误发生在这一行:

return json.Unmarshal(resBody, v)

The error indicates that the server did not return a valid JSON response.该错误表明服务器未返回有效的 JSON 响应。 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
}

Reason for this eerie error message is :此怪异错误消息的原因是:

// When unmarshaling quoted strings, invalid UTF-8 or
// invalid UTF-16 surrogate pairs are not treated as an error.
// Instead, they are replaced by the Unicode replacement
// character U+FFFD.

https://golang.org/src/encoding/json/decode.go https://golang.org/src/encoding/json/decode.go

In my case I saved my json as string then parsed it by : stringData = JSON.parse(myJsonString)在我的情况下,我将我的 json 保存为字符串,然后通过以下stringData = JSON.parse(myJsonString)解析它: stringData = JSON.parse(myJsonString)

I also had the same error another time using gin-context-ShouldBind() ( https://godoc.org/github.com/gin-gonic/gin#Context.ShouldBind ) and mapping my json to go object: error was because it needs a json as string, so I used : JSON.stringify(jsonObject) when sending my request from front-end part.我在另一次使用 gin-context-ShouldBind() ( https://godoc.org/github.com/gin-gonic/gin#Context.ShouldBind ) 并将我的 json 映射到 go 对象时也遇到了同样的错误:错误是因为它需要一个 json 作为字符串,所以我在从前端部分发送我的请求时使用了:JSON.stringify(jsonObject)。

Please Check the request header first.请先检查请求头。 Check the Content-Type.检查内容类型。 In my cases, it's not an application/JSON .在我的情况下,它不是application/JSON It's application/x-www-form-urlencoded.它是application/x-www-form-urlencoded.

I have the problem, too.我也有问题It's because vue request api stringifies the json data, keep request data is json data.是因为vue request api对json数据进行了字符串化处理,保持request数据为json数据。 like curl apiUrl -H '' -d '{"a":1,"b":2}' , not curl apiUrl -H '' -d 'a=1&b=2' , I think keep your response is json data will be resolvedcurl apiUrl -H '' -d '{"a":1,"b":2}' ,而不是curl apiUrl -H '' -d 'a=1&b=2' ,我认为保持你的回应是 json 数据将得到解决

Use this option, -H "Content-Type: application/json", while making a POST request.在发出 POST 请求时使用此选项 -H "Content-Type: application/json"。
Also use bash shell rather than cmd or powershell for POST request.对于 POST 请求,还可以使用 bash shell 而不是 cmd 或 powershell。 cmd doesnt support single quote. cmd 不支持单引号。 You need to escape the double quotes with \\ inside json content for cmd您需要在 cmd 的 json 内容中使用 \\ 转义双引号

I had this same problem.我有同样的问题。 My mistake was very specific to what I was doing.我的错误与我正在做的事情非常具体。 I was formatting my string, but didn't put the quotes for the substituted variables.我正在格式化我的字符串,但没有为替换变量加上引号。

The body I was putting in was this我放入的身体是这个

respBody := fmt.Sprintf(`{"a": %s, "b": %s}`, "a", "b")

This was formatted to这被格式化为

{"a": a, "b": b}

I didn't realize I needed to add the quotes.我没有意识到我需要添加引号。 It needs to be like它需要像

respBody := fmt.Sprintf(`{"a": "%s", "b": "%s"}`, "a", "b")

This was formatted to这被格式化为

{"a": "a", "b": "b"}

The offset was referring to the first char of the unquoted string.偏移量是指未加引号的字符串的第一个字符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “无效字符'u'寻找值的开始”使用golang开发的服务解析错误 - “invalid character 'u' looking for beginning of value”Parsing Error from an Service developed in golang golang websocket.JSON.Receive无效字符'q'寻找值的开头 - golang websocket.JSON.Receive invalid character 'q' looking for beginning of value Parse.com说“寻找值的开头的无效字符'\\” - Parse.com says “invalid character '\'' looking for beginning of value” Xamarin&Golang-{[]}无效字符'\\ x00'寻找对象密钥字符串的开头 - Xamarin & Golang - { []} invalid character '\x00' looking for beginning of object key string 解组 JSON 返回错误:无效字符 '\\x1f' 寻找值的开头 - Unmarshalling JSON returns an error: invalid character '\x1f' looking for beginning of value 从精读的HTTP中解组JSON:无效字符寻找值的开头 - Go unmarshalling JSON from compessed HTTP: invalid character looking for beginning of value JSON无效字符'}'正在寻找对象键字符串的开头 - JSON invalid character '}' looking for beginning of object key string json 格式无效,请检查。 原因:寻找对象键字符串开头的无效字符“a” - Invalid json format, please check. Reason: invalid character 'a' looking for beginning of object key string JSON int密钥发布数据e2e(寻找对象密钥字符串开头的无效字符“ 1”) - JSON int key issue data e2e (invalid character '1' looking for beginning of object key string) JSON无法写入创世纪块:无效字符'\\\\'寻找对象密钥字符串的开头 - JSON failed to write genesis block: invalid character '\\' looking for beginning of object key string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM