beego api post插入数据

首先。我使用邮递员来发布数据。如何从邮递员密钥中获取数据。它通过使用这种方式工作(在我的代码中我自己设置它)。


我想在表单数据中获取名字、姓氏和电子邮件。我想使用如下。


func InsertOneUser(user User) *User {

   o := orm.NewOrm()

   qs := o.QueryTable(new(User))

   i, _ := qs.PrepareInsert()

   var u User

   user.FirstName = "firstname"                          <----- this

   user.LastName = "lastname"                            <----- this

   user.Email = "something@yahoo.com"                    <----- this

   user.Password, _ = hashPassword(user.Password)

   user.RegDate = time.Now()

   id, err := i.Insert(&user)

   return &u

}

http://img.mukewang.com/6486dc4600010d0410510732.jpg

Cats萌萌
浏览 206回答 1
1回答

一只甜甜圈

你需要在 func 中使用beego.Controller。如果func有beego.Controller,有两种方法(key, parseform)。package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "github.com/astaxie/beego")type User struct {&nbsp; &nbsp; Firstname string `form:"firstname"`&nbsp; &nbsp; Lastname&nbsp; string `form:"lastname"`&nbsp; &nbsp; Email&nbsp; &nbsp; &nbsp;string `form:"email"`}type MainController struct {&nbsp; &nbsp; beego.Controller}func (this *MainController) Post() {&nbsp; &nbsp; // using key&nbsp; &nbsp; firstname := this.GetString("firstname")&nbsp; &nbsp; lastname := this.GetString("lastname")&nbsp; &nbsp; email := this.GetString("email")&nbsp; &nbsp; // using Parseform&nbsp; &nbsp; u := User{}&nbsp; &nbsp; if err := this.ParseForm(&u); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(err)&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Println(u)&nbsp; &nbsp; this.Ctx.WriteString(fmt.Sprintf("%s %s %s", firstname, lastname, email))}func main() {&nbsp; &nbsp; beego.Router("/api/v1/user", &MainController{})&nbsp; &nbsp; beego.Run()}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go