一、获取参数
GetString(key string) string
GetStrings(key string) []string
GetInt(key string) (int64, error)
GetBool(key string) (bool, error)
GetFloat(key string) (float64, error)
var data = make(map[string]interface{
})
input, _ := c.Input()
for k, v := range input {
data[k] = v[0]
}
if id, err := models.AdminInstall(data); err != nil {
c.Data["json"] = ReturnJson(0, err.Error(), "")
} else {
c.Data["json"] = ReturnJson(200, "OK", id)
}
c.ServeJSON()
二、直接解析到 struct。struct tag 里 要有 form 字段
m := models.Admin{
}
if err := c.ParseForm(&m); err != nil {
fmt.Println(err.Error())
}
if id, err := models.AdminInsert(&m); err != nil {
c.Data["json"] = ReturnJson(0, err.Error(), "")
} else {
c.Data["json"] = ReturnJson(200, "OK", id)
}
c.ServeJSON()
三、获取 Request Body 里的内容
在配置文件里设置 copyrequestbody = true
var ob models.Object
var err error
if err = json.Unmarshal(this.Ctx.Input.RequestBody, &ob); err == nil {
objectid := models.AddOne(ob)
this.Data["json"] = "{\"ObjectId\":\"" + objectid + "\"}"
} else {
this.Data["json"] = err.Error()
}
this.ServeJSON()