Beego框架之請求資料處理
阿新 • • 發佈:2018-11-26
我們經常需要獲取使用者傳遞的資料,包括 Get、POST 等方式的請求,beego 裡面會自動解析這些資料,你可以通過如下方式獲取資料:
通過this.Getstring("獲取使用者輸入")獲取使用者輸入
再通過this.Ctx.WriteString("輸出使用者輸入的內容")輸出使用者輸入的內容
通過Input.Get("")獲取使用者輸入
直接解析到struct
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("<html>" + id + "<br/>") //name := c.Input().Get("name") //c.Ctx.WriteString(name + "</html>") c.Ctx.WriteString(`<html><form action="http://127.0.0.1:8080/test_input" method="post"> <input type="text" name="Username"/> <input type="password" name="Password"/> <input type="submit" value="提交"/> </form></html>`) } func (c *TestInputController) Post(){ u := User{} if err:=c.ParseForm(&u) ; err != nil{ //process error } c.Ctx.WriteString("Username:" + u.Username + " Password:" + u.Password) }