Http Beego POST请求正文始终为空
我正在使用Beego的便捷方法解析请求体值,并具有以下功能: 路由器文件:Http Beego POST请求正文始终为空,http,go,superagent,beego,Http,Go,Superagent,Beego,我正在使用Beego的便捷方法解析请求体值,并具有以下功能: 路由器文件: apiNamespace := beego.NewNamespace("/api") apiNamespace.Router("/sessions/google/new", &controllers.SessionsController{}, "get:GoogleNewSession") beego.AddNamespace(apiNamespace) 控制器代码: func (c
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(超级代理):
当超级代理请求触发时,我可以在日志中看到路径得到了正确匹配。但是,c.Ctx.Input.RequestBody
始终为空
我试着用别的东西来激发请求,比如邮递员,但没有用。在GET请求中,我能够正确检索查询参数
有任何线索或建议可以帮助修复或调试此问题吗?您需要在配置文件“conf/app.conf”中配置“copyrequestbody=true”
默认值为false,因此内容不会复制到c.Ctx.Input.RequestBody
该示例显示在文档中的“从请求体检索数据”部分。()您可以进行HTTP捕获以确保请求正文不为空吗?heyo您建议我怎么做?在chrome的开发者网络控制台中,我检查了主体是否包含有效负载,例如{“code”:“123”}但是当在Beego上打印整个请求时,它是空的:(所以你得到了print
Received[]
?@jiangd yep当试图使用JSON主体时,它似乎总是空的。然而,我尝试将标题更改为.set)('Content-Type','application/x-www-form-urlencoded')
并且我能够检索c.Ctx.Input.Request.form.Get(“code”)
中的字段。知道为什么会这样吗?为什么不将Content-Type设置为json?我看到了您的代码。设置('Accept','application/json'))
,但不是内容类型
。这就是解决方案!你不知道我用谷歌搜索了多少次才得到这个答案!
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);
}
});