Search code examples
gobeego

Functionality of Beego syntax 'Ctx.Input.GetData('<variable-name>')'


I am new to beego and goLang. I came across a code. If someone could explain the flow it would be very helpful. It is a GET API. I think Prepare() is like a filter. What I don't understand is c.Ctx.Input.GetData("customerid") and c.Ctx.Input.GetData("customergroupid") functions. Can someone explain me what GetData is doing and how we can pass values to them?

// URLMapping ...
func (c *CampusHomeController) URLMapping() {
    c.Mapping("GetOne", c.GetOne)
}
func (c *CampusHomeController) Prepare() {
    if c.Ctx.Input.GetData("customerid") == "" {
        returnJSON := postCampusHomeJSON{}
        returnJSON.Code = 403
        returnJSON.Msg = "Invalid Session"
        c.Data["json"] = &returnJSON
        c.ServeJSON()
        c.StopRun()
    }

    if c.Ctx.Input.GetData("customergroupid") == "" ||             
        c.Ctx.Input.GetData("customergroupid") == nil {

        returnJSON := postCampusHomeJSON{}
        returnJSON.Code = 404
        returnJSON.Msg = "User not a campus manager"
        c.Data["json"] = &returnJSON
        c.ServeJSON()
        c.StopRun()
    }
}

Solution

  • GetData is used to get data from filters in your controllers. It allows you to pass values other than just strings.

    From the Beego documentation: GetData Get value of Data in Input

    SetData Set value of Data in Input. GetData and SetData is used to pass data from Filter to Controller https://beego.me/docs/module/context.md