本文整理汇总了Golang中github.com/app-kit/go-appkit.Request.ParseJsonData方法的典型用法代码示例。如果您正苦于以下问题:Golang Request.ParseJsonData方法的具体用法?Golang Request.ParseJsonData怎么用?Golang Request.ParseJsonData使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/app-kit/go-appkit.Request
的用法示例。
在下文中一共展示了Request.ParseJsonData方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: UnserializeRequestMiddleware
func UnserializeRequestMiddleware(registry kit.Registry, request kit.Request) (kit.Response, bool) {
// Try to parse json in body. Ignore error since body might not contain json.
contentType := request.GetHttpRequest().Header.Get("Content-Type")
if strings.Contains(contentType, "json") {
// Only read the HTTP body automatically for json content type requests,
// since some handlers might need to read it themselfes (see the files package resource).
if err := request.ReadHttpBody(); err != nil {
return kit.NewErrorResponse(err, "http_body_read_error"), false
} else {
if request.GetRawData() != nil {
if err := request.ParseJsonData(); err != nil {
return kit.NewErrorResponse(err, "invalid_json_body", true), false
}
if request.GetData() != nil {
// Successfully parsed json body.
// Now try to unserialize.
// Determine serializer.
serializer := registry.DefaultSerializer()
// Check if a custom serializer was specified.
if name := request.GetContext().String("request-serializer"); name != "" {
serializer = registry.Serializer(name)
}
if serializer == nil {
return kit.NewErrorResponse("unknown_serializer", fmt.Sprintf("The specified request serializer does not exist")), false
} else {
if err := request.Unserialize(serializer); err != nil {
return kit.NewErrorResponse(err, "request_unserialize_error", true), false
}
}
}
}
}
}
return nil, false
}