我们经常需要获取用户传递的数据,包括 Get、POST 等方式的请求,beego 里面会自动解析这些数据,你可以通过如下方式获取数据:

Beego框架之请求数据处理_第1张图片

通过this.Getstring("获取用户输入")获取用户输入
再通过this.Ctx.WriteString("输出用户输入的内容")输出用户输入的内容

Beego框架之请求数据处理_第2张图片

通过Input.Get("")获取用户输入

Beego框架之请求数据处理_第3张图片

package controllers

import (
    "github.com/astaxie/beego"
)

type TestOnceController struct {
    beego.Controller
}

/*type User struct {
    Username string
    Password string
}
*/
func (c *TestOnceController) Get()  {
    id := c.GetString("id")
    c.Ctx.WriteString("" + id + "
") name := c.Input().Get("name") c.Ctx.WriteString(name + "") }

执行结果
Beego框架之请求数据处理_第4张图片


直接解析到struct

Beego框架之请求数据处理_第5张图片


controller testInputController
router beego.Router("/test_input", &controllers.TestInputController{}, "get:Get;post:Post")

package controllers

import (
    "github.com/astaxie/beego"
)

type TestInputController struct {
    beego.Controller
}

type User struct{
    Username string
    Password string
}

func (c *TestInputController) Get(){
    //id := c.GetString("id")
    //c.Ctx.WriteString("" + id + "
") //name := c.Input().Get("name") //c.Ctx.WriteString(name + "") c.Ctx.WriteString(`
`) } func (c *TestInputController) Post(){ u := User{} if err:=c.ParseForm(&u) ; err != nil{ //process error } c.Ctx.WriteString("Username:" + u.Username + " Password:" + u.Password) }