简体   繁体   中英

Golang invalid character 'b' looking for beginning of value

I am trying to post json with a 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 the 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
}

The error happened at this line:

return json.Unmarshal(resBody, v)

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
}

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.

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.

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

Use this option, -H "Content-Type: application/json", while making a POST request.
Also use bash shell rather than cmd or powershell for POST request. cmd doesnt support single quote. You need to escape the double quotes with \\ inside json content for cmd

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM