原文: 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)
-