nig*_*ent 5 http go superagent beego
我正在使用Beego的便捷方法来解析请求正文值,并具有以下内容:
路由器文件:
apiNamespace := beego.NewNamespace("/api")
apiNamespace.Router("/sessions/google/new", &controllers.SessionsController{}, "get:GoogleNewSession")
beego.AddNamespace(apiNamespace)
Run Code Online (Sandbox Code Playgroud)
控制器代码:
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
}
Run Code Online (Sandbox Code Playgroud)
前端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);
}
});
Run Code Online (Sandbox Code Playgroud)
当superagent请求触发时,我可以在日志中看到路径正确匹配.但是,c.Ctx.Input.RequestBody总是空的.
我曾尝试使用其他东西来解决邮递员等请求,但无济于事.在GET请求中,我能够正确检索查询参数.
有关帮助修复或调试此问题的任何线索或建议吗?
小智 11
您需要在配置文件"conf/app.conf"中配置"copyrequestbody = true".
默认值为false,因此内容不会复制到c.Ctx.Input.RequestBody.
该示例显示在文档中的"从请求正文中检索数据"部分.(http://beego.me/docs/mvc/controller/params.md)