0
votes

I am using go-gin as server and trying to decode the request body. When I send request which has both the strings

{
    "name": "abc"
}

The following code decodes it correctly:

var decodedBody map[string]string
err = json.NewDecoder(c.Request.Body).Decode(&decodedBody)

But if I send

{
    "id": 1
}

The following code gives me a blank map

var decodedBody map[string]int
err = json.NewDecoder(c.Request.Body).Decode(&decodedBody)

Not sure what am I missing here. Any pointers?

1
What does the error say? - Tim Cooper
No error fmt.Println(decodedBody) gives me map[] instead of map[id:1]. The first decoded body is giving me map[name:abc] correctly - codec
Coudn't reproduce play.golang.org/p/YzdXR90x0O. Please provide a MVCE. - hlscalon

1 Answers

0
votes

because you set the decodeBody's data type with string,if your value is not the string value, it will not decode the correct value,{"id":1},it's value's type is int,not the string.