beego框架之请求数据处理
我们经常需要获取用户传递的数据,包括 Get、POST 等方式的请求,beego 里面会自动解析这些数据,你可以通过如下方式获取数据:
直接解析到 struct
packagecontrollersimport("github.com/astaxie/beego""log")typeTestInputControllerstruct{beego.Controller}func(c*TestInputController)Get(){id:=c.GetString("id")c.Ctx.WriteString("id:"+id)u:=user{}iferr:=c.ParseForm(&u);err!=nil{}c.Ctx.WriteString("Username:"+u.Username+"Password:"+u.Password)}typeuserstruct{UsernamestringPasswordstring}func(c*TestInputController)Post(){u:=user{}//获取form表单传过来的值iferr:=c.ParseForm(&u);err!=nil{log.Fatal(err)}c.Ctx.WriteString("Username:"+u.Username+"Password:"+u.Password)}
获取 Request Body 里的内容
1. 在配置文件里设置 copyrequestbody = true
2. 在 Controller 中
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。