Input

In general, processing input relies heavily on the methods provided by the Controller. And specific inputs can be found in:

  • Path variable: refer to router
  • Query parameter
  • Body: To read data from the request body, int most cases setting BConfig.CopyRequestBody to true is enough. If you create more than one web.Server, then you must set CopyRequestBody to true in each Server instance

And the methods of obtaining parameters can be divided into two main categories:

  • The first category is methods prefixed with Get: this is a large category of methods that try to get the value of a particular parameter
  • The second category is methods prefixed with Bind: this is a large category of methods that attempt to convert input into a structure

Get

For this type of method, Beego reads from two main places: the query parameters and the form, and if there are parameters with the same name in both places, then Beego returns the data inside the form. For example:

  1. type MainController struct {
  2. web.Controller
  3. }
  4. func (ctrl *MainController) Post() {
  5. name := ctrl.GetString("name")
  6. if name == "" {
  7. ctrl.Ctx.WriteString("Hello World")
  8. return
  9. }
  10. ctrl.Ctx.WriteString("Hello " + name)
  11. }

When we access.

  • Path localhost:8080?name=a: it will output Hello, a
  • path localhost:8080, and then the form is submitted with name=b, then the output will be b
  • path localhost:8080?name=a, and the form is submitted with name=b, then b will be output

Methods in this category also allow default values to be passed in:

  1. func (ctrl *MainController) Get() {
  2. name := ctrl.GetString("name", "Tom")
  3. ctrl.Ctx.WriteString("Hello " + name)
  4. }

If we don’t pass the name parameter, then Tom will be used as the value of name, for example when we access GET localhost:8080, it will output Hello Tom.

It should be noted that the method signature of GetString is:

  1. func (c *Controller) GetString(key string, def ...string) string {
  2. // ...
  3. }

Note that although def is declared as a variable parameter, in practice, Beego will only use the first default value, and all subsequent ones will be ignored.

The method signatures and behaviors are similar for this class, they are:

  • GetString(key string, def ...string) string
  • GetStrings(key string, def ...[]string) []string
  • GetInt(key string, def ...int) (int, error)
  • GetInt8(key string, def ...int8) (int8, error)
  • GetUint8(key string, def ...uint8) (uint8, error)
  • GetInt16(key string, def ...int16) (int16, error)
  • GetUint16(key string, def ...uint16) (uint16, error)
  • GetInt32(key string, def ...int32) (int32, error)
  • GetUint32(key string, def ...uint32) (uint32, error)
  • GetInt64(key string, def ...int64) (int64, error)
  • GetUint64(key string, def ...uint64) (uint64, error)
  • GetBool(key string, def ...bool) (bool, error)
  • GetFloat(key string, def ...float64) (float64, error)

Note that GetString and GetStrings themselves are not designed to return error, so you can’t get an error.

Bind

Most of the time, we also need to convert the input to a structure, and Beego provides a series of methods to do the input-to-structure binding.

This part of the method is defined directly on the Context structure, so the user can manipulate the Context instance directly. To simplify the operation, we have defined similar methods on the Controller.

  1. // 要设置 web.BConfig.CopyRequestBody = true
  2. type MainController struct {
  3. web.Controller
  4. }
  5. func (ctrl *MainController) Post() {
  6. user := User{}
  7. err := ctrl.BindJSON(&user)
  8. if err != nil {
  9. ctrl.Ctx.WriteString(err.Error())
  10. return
  11. }
  12. ctrl.Ctx.WriteString(fmt.Sprintf("%v", user))
  13. }
  14. type User struct {
  15. Age int `json:"age"`
  16. Name string `json:"name"`
  17. }
  • Bind(obj interface{}) error: Based on Content-Type
  • BindYAML(obj interface{}) error
  • BindForm(obj interface{}) error
  • BindJSON(obj interface{}) error
  • BindProtobuf(obj proto.Message) error
  • BindXML(obj interface{}) error

Note that although we provide a way to determine how to bind based on Content-Type, we prefer users to use the bind method that specifies the format.

An API that should only accept input in a particular format, such as JSON only, and should not be able to handle multiple inputs

In the early days, Beego also had a method similar to BindForm: ParseForm(obj interface{}) error, both of which have the same effect.

Path Variable

Refer Path Variable Router

Historical Bind methods

In Beego’s Input there is a family of methods defined for reading parameters. This class of methods is very similar to the Get family of methods.

Example:

  1. ?id=123&isok=true&ft=1.2&ol[0]=1&ol[1]=2&ul[]=str&ul[]=array&user.Name=astaxie
  1. var id int
  2. this.Ctx.Input.Bind(&id, "id") //id ==123
  3. var isok bool
  4. this.Ctx.Input.Bind(&isok, "isok") //isok ==true
  5. var ft float64
  6. this.Ctx.Input.Bind(&ft, "ft") //ft ==1.2
  7. ol := make([]int, 0, 2)
  8. this.Ctx.Input.Bind(&ol, "ol") //ol ==[1 2]
  9. ul := make([]string, 0, 2)
  10. this.Ctx.Input.Bind(&ul, "ul") //ul ==[str array]
  11. user struct{Name}
  12. this.Ctx.Input.Bind(&user, "user") //user =={Name:"astaxie"}

Reference