我正在使用Beego的便捷方法解析请求体值,并具有以下内容:
路由器文件:
apiNamespace := beego.NewNamespace("/api")
apiNamespace.Router("/sessions/google/new", &controllers.SessionsController{}, "get:GoogleNewSession")
beego.AddNamespace(apiNamespace)
控制器代码:
func (c *SessionsController) URLMapping() {
c.Mapping("GoogleNewSession", c.GoogleNewSession)
}
func (c *SessionsController) GoogleNewSession() {
// Always serve JSON
defer func() {
c.ServeJson()
}()
// This is always blank
log.Printf("'Received %+v'", c.Ctx.Input.RequestBody)
c.Ctx.ResponseWriter.WriteHeader(200)
return
// truncated
}
前端JS(超级代理):
request
.post('/sessions/google/new')
.use(prefix)
.send({ code: authCode })
.set('Accept', 'application/json')
.end(function(err, res){
console.log("******* request", res.request)
if (res.ok) {
var body = res.body;
console.log('yay got ' + JSON.stringify(res.body));
} else {
console.log("***** err", err);
console.log("***** not ok", res.text);
}
});
当superagent请求触发时,我可以在日志中看到路径被正确匹配。然而,
c.Ctx.Input.RequestBody
始终为空。
我尝试使用其他工具比如Postman发送请求,但是没有成功。在GET请求中,我能够正确获取查询参数。
有什么提示或建议可以帮助修复或调试此问题吗?
Received []
吗? - Jiang YD.set('Content-Type', 'application/x-www-form-urlencoded')
,然后我可以在c.Ctx.Input.Request.Form.Get("code")
中检索字段。有什么想法是这种情况吗? - rawfish.dev.set('Accept', 'application/json')
,但没有Content-Type
。 - Jiang YD