Beego POST request body always empty
Asked Answered
S

1

7

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?

Sublimity answered 22/6, 2015 at 14:43 Comment(6)
can you make a HTTP capture to make sure the request body is not empty?Oenone
heyo how do you suggest i go about that? In chrome's developer network console i've checked to see that the body includes the payload e.g {"code": "123"} but when printing the entire request on Beego it's empty :(Sublimity
so you got print Received []?Oenone
@JiangYD yep when attempting to use a JSON body it always seems to be empty. However, I've tried changing the header to .set('Content-Type', 'application/x-www-form-urlencoded') and I am able to retrieve the fields in the c.Ctx.Input.Request.Form.Get("code"). Any idea why this is the case?Sublimity
why you not set content-type to json then? i saw your code .set('Accept', 'application/json'), but not Content-Type.Oenone
I missed it out in the original post but i've actually tried it already without any luck. I've moved off beego to gin and the exact same superagent code works perfectly. No idea what else I did wrong but seems like too much effort to debug for a simple json POST unwrap. Thanks for trying!Sublimity
C
17

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)

Croom answered 6/8, 2015 at 0:32 Comment(2)
this is the solution !Ternan
you have no idea how much Googling I have done to get to this answer!Leodora

© 2022 - 2024 — McMap. All rights reserved.