如何使用 Beego 验证 POST 请求中的 JSON 消息
How to validate the JSON message in a POST request with Beego
我正在使用 Beego 开发 Web 服务器。
我使用 beego.Controller
来处理 POST 请求。在我的例子中,POST 请求包含一个 JSON:
{
"name": "titi",
"password": "123456"
}
这是我的代码:
type TestController struct {
beego.Controller
}
type User struct {
Name string `json:"name"`
Password string `json:"password"`
}
func (c *TestController) Post() {
var ob md.User
var err error
if err = json.Unmarshal(c.Ctx.Input.RequestBody, &ob); err == nil {
logs.Debug(ob.Name)
logs.Debug(len(ob.Name))
} else {
logs.Error("illegal JSON")
}
}
这段代码工作正常。借助结构 User
的标签,"name"
被分配给 ob.Name
并且 "password"
被分配给 ob.Password
.
现在,我想测试一些异常情况。例如,如果 JSON 请求不包含预期的密钥怎么办:
{
"illegalKey1": "titi",
"illegalKey2": "123456"
}
如您所见,我期待 "name"
和 "password"
,但现在密钥变成了 "illegalKey1"
和 "illegalKey2"
。所以我认为这可能会导致一些错误。
令我惊讶的是,没有任何错误,因为 err == nil
仍然为真,而 len(ob.Name)
现在变成了 0。
那么在Go/Beego中有什么好的方法可以处理这种情况吗?
我的意思是,我知道
if len(ob.Name) == 0 {
logs.Error("illegal JSON")
}
可以,但我想知道是否有更漂亮的代码?否则,如果JSON中有10个字段,我就得做10次这样的if
。显然这一点都不好
要确保 JSON 不包含意外字段,您可以使用 "encoding/json"
包中的 Decoder
及其方法 DisallowUnknownFields. See example here https://play.golang.org/p/bif833qxytE
请注意 json.NewDecoder
需要 io.Reader
作为输入。您可以使用 bytes.NewReader
从 []byte
创建 io.Reader
另一个主题是确保 JSON 包含所有预期的字段(或者这些字段采用特定格式)。如何让它与 "encoding/json"
包一起工作的答案是为结构实现自定义 UnmarshalJSON
,可以找到 here. But I would not suggest using this approach for such task, because basically this is a validation of an input and I would rather use validation packages for this not to mix responsibilities. The most common one is go-playground/validator.v9 where you should look for required
tag. Validation cases are discussed here.
我正在使用 Beego 开发 Web 服务器。
我使用 beego.Controller
来处理 POST 请求。在我的例子中,POST 请求包含一个 JSON:
{
"name": "titi",
"password": "123456"
}
这是我的代码:
type TestController struct {
beego.Controller
}
type User struct {
Name string `json:"name"`
Password string `json:"password"`
}
func (c *TestController) Post() {
var ob md.User
var err error
if err = json.Unmarshal(c.Ctx.Input.RequestBody, &ob); err == nil {
logs.Debug(ob.Name)
logs.Debug(len(ob.Name))
} else {
logs.Error("illegal JSON")
}
}
这段代码工作正常。借助结构 User
的标签,"name"
被分配给 ob.Name
并且 "password"
被分配给 ob.Password
.
现在,我想测试一些异常情况。例如,如果 JSON 请求不包含预期的密钥怎么办:
{
"illegalKey1": "titi",
"illegalKey2": "123456"
}
如您所见,我期待 "name"
和 "password"
,但现在密钥变成了 "illegalKey1"
和 "illegalKey2"
。所以我认为这可能会导致一些错误。
令我惊讶的是,没有任何错误,因为 err == nil
仍然为真,而 len(ob.Name)
现在变成了 0。
那么在Go/Beego中有什么好的方法可以处理这种情况吗?
我的意思是,我知道
if len(ob.Name) == 0 {
logs.Error("illegal JSON")
}
可以,但我想知道是否有更漂亮的代码?否则,如果JSON中有10个字段,我就得做10次这样的if
。显然这一点都不好
要确保 JSON 不包含意外字段,您可以使用 "encoding/json"
包中的 Decoder
及其方法 DisallowUnknownFields. See example here https://play.golang.org/p/bif833qxytE
请注意 json.NewDecoder
需要 io.Reader
作为输入。您可以使用 bytes.NewReader
[]byte
创建 io.Reader
另一个主题是确保 JSON 包含所有预期的字段(或者这些字段采用特定格式)。如何让它与 "encoding/json"
包一起工作的答案是为结构实现自定义 UnmarshalJSON
,可以找到 here. But I would not suggest using this approach for such task, because basically this is a validation of an input and I would rather use validation packages for this not to mix responsibilities. The most common one is go-playground/validator.v9 where you should look for required
tag. Validation cases are discussed here.