简体   繁体   English

杜松子酒拦截请求正文

[英]Go-gin intercepting a request body

I am using go-gin as server and trying to decode the request body. 我正在使用go-gin作为服务器,并尝试解码请求正文。 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? 有指针吗?

因为您使用字符串设置了encodeBody的数据类型,所以如果您的值不是字符串值,则不会解码正确的值{“ id”:1},它的值的类型是int,而不是字符串。

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

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