6

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?

rawfish.dev
  • 319
  • 3
  • 13
  • 1
    can you make a HTTP capture to make sure the request body is not empty? – Jiang YD Jun 23 '15 at 05:39
  • 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 :( – rawfish.dev Jun 23 '15 at 09:09
  • so you got print `Received []`? – Jiang YD Jun 24 '15 at 01:32
  • @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? – rawfish.dev Jun 25 '15 at 16:19
  • why you not set content-type to json then? i saw your code `.set('Accept', 'application/json')`, but not `Content-Type`. – Jiang YD Jun 26 '15 at 00:21
  • 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! – rawfish.dev Jun 28 '15 at 01:57

1 Answers1

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)

Edcyc
  • 196
  • 3