Beego框架POST请求接收JSON数据

  • 在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)
}

 

你可能感兴趣的:(Beego框架POST请求接收JSON数据)