Beego的POST请求体始终为空。

huangapple go评论71阅读模式
英文:

Beego POST request body always empty

问题

我正在使用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() {

    // 始终返回 JSON
    defer func() {
        c.ServeJson()
    }()

    // 这里始终为空
    log.Printf("'Received %+v'", c.Ctx.Input.RequestBody)

    c.Ctx.ResponseWriter.WriteHeader(200)
    return

    // 截断的代码
}

前端 JS (super-agent):

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 请求中,我能够正确获取查询参数。

有什么线索或建议可以帮助解决或调试这个问题吗?

英文:

I'm working with Beego's convenience methods for parsing request body values and have the following:

Router file:

    apiNamespace := beego.NewNamespace("/api")

	apiNamespace.Router("/sessions/google/new", &controllers.SessionsController{}, "get:GoogleNewSession")

	beego.AddNamespace(apiNamespace)

Controller code:

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
}

Front end JS (super-agent):

    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);
		 }
     });

When the superagent request fires off, I can see in the logs that the path is getting correctly matched. However, the c.Ctx.Input.RequestBody is always empty.

I have tried using something else to fire the request such as Postman but to no avail. In GET requests I am able to retrieve query params correctly.

Any clues or suggestions to help fix or debug this issue?

答案1

得分: 17

你需要在配置文件"conf/app.conf"中配置"copyrequestbody = true"。

默认值为false,因此内容不会被复制到c.Ctx.Input.RequestBody中。

示例在文档的"从请求体中获取数据"部分中展示。(http://beego.me/docs/mvc/controller/params.md)

英文:

You need to configure "copyrequestbody = true" in configuration file "conf/app.conf".

The default is false so the content is not copied to c.Ctx.Input.RequestBody.

The example is shown section "Retrieving data from request body" in the document. (http://beego.me/docs/mvc/controller/params.md)

huangapple
  • 本文由 发表于 2015年6月22日 22:43:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/30982891.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定