golang invalid character 'b' looking for beginning of value

后端 未结 4 1845
半阙折子戏
半阙折子戏 2021-01-04 07:35

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

相关标签:
4条回答
  • 2021-01-04 08:02

    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

    In my case I saved my json as string then parsed it by : 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.

    0 讨论(0)
  • 2021-01-04 08:05

    Please Check the request header first. Check the Content-Type. In my cases, it's not an application/JSON. It's application/x-www-form-urlencoded.

    0 讨论(0)
  • 2021-01-04 08:06

    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
    }
    
    0 讨论(0)
  • 2021-01-04 08:10

    I have the problem, too. It's because vue request api stringifies the json data, keep request data is json data. 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 resolved

    0 讨论(0)
提交回复
热议问题