原文: https://blog.csdn.net/Aaron_80726/article/details/83870563
--------------------------------------------
Beego框架POST请求接收JSON数据
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Aaron_80726/article/details/83870563
- 在app.conf配置文件中设置 copyrequestbody = true
copyrequestbody = true
- 在controller中使用Golang标准库json包来解析json数据封装到stuct结构体
-
package controllers
-
-
import (
-
"encoding/json"
-
"fmt"
-
"github.com/astaxie/beego"
-
)
-
-
-
type UserController struct {
-
beego.Controller
-
}
-
-
type User struct {
-
Id string
-
Name string
-
Pwd string
-
}
-
-
func (this *UserController) AddUser() {
-
var user User
-
data := this.Ctx.Input.RequestBody
-
//json数据封装到user对象中
-
err := json.Unmarshal(data, &user)
-
if err != nil {
-
fmt.Println("json.Unmarshal is err:", err.Error())
-
}
-
fmt.Println(user)
-
this.Ctx.WriteString(user.Name)
-