Beego how to get user request parameters

Source: Internet
Author: User
Tags key string
This is a creation in Article, where the information may have evolved or changed.

Get parameters

We often need to get user-transmitted data, including get, POST and other ways of request, Beego inside will automatically parse the data, you can get the data by the following ways:

    • getstring (key string) STRING
    • getstrings (key string) []string
    • getint (Key String) (Int64, error)
    • Getbool (Key string) (bool, error)
    • getfloat (Key string ) (float64, error)

Examples of use are as follows:

func( This *Maincontroller) Post() {Jsoninfo:=  This.GetString("Jsoninfo")    ifJsoninfo== "" {         This.Ctx.writestring("Jsoninfo is empty")        return    }}

If the data you need may be of other types, such as the int type instead of int64, then you need to do this:

func( This *Maincontroller) Post() {ID:=  This.Input().Get("id")IntID,Err:=StrConv.Atoi(ID)}

For more information about the request, the user can this.Ctx.Request refer to the manual request by obtaining information about the properties and methods of the object.

Resolve directly to struct

If you want to assign the contents of the form to a struct, in addition to one of the above methods to get the re-assignment, Beego provides a more convenient way, through the struct's field name or tag and the form fields correspond directly to the struct.

Define a struct:

Type Userstruct {    Id    int         ' form: '-' '    Name  Interface{} ' form: ' username '//corresponds to the name value in the form, the first letter of the field name must also be uppercase, otherwise the value of the parameter cannot be resolved     Age   int         ' form: ' Age '//corresponds to the name value in the form, and the value of the parameter cannot be resolved if not written    Email string}

Form:

<form ID="User">Name:<input name="username" type="Text" />Age:<input name="Age" type="Text" />Email:<input name="Email" type="Text" />    <input type="Submit" value="Submit" /></form>

Controller in the analysis:

func( This *Maincontroller) Post() {u:=User{}    ifErr:=  This.Parseform(&u);Err!= Nil {        //handle Error    }}

Attention:

  • Structtag the definition of a form and the RenderForm method share a label
  • When a struct is defined, the field name will be assigned to the field if it has a form, which assigns the name of the form and the tag in the same field, or the form table dropdowns to the field name. As shown in the example above, the username and age in the form are assigned to the Name and the aged field in the user, and the content in the email is assigned to the email field.
  • When calling Controller Parseform this method, the passed parameter must be a pointer to a struct, otherwise the assignment to the struct will not succeed and return xx must be a struct pointer an error.
  • If you want to omit a field, there are two ways to do this: The field name starts with lowercase, and the second is: form the value of the label is set to-

Original address: Https://beego.me/docs/mvc/controller/params.md
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.