向golang Fiber发送json数据,尝试将接收到的数据绑定到struct,但是失败了,
type MapTag struct {
Id uint `json:"id" form:"tag_name" xorm:"bigint unsigned pk autoincr"`
TagName string `json:"tag_name" form:"tag_name" xorm:"index"`
CreatedAt time.Time `json:"created_at" form:"created_at" xorm:"timestamp created"`
UpdatedAt time.Time `json:"updated_at" form:"updated_at" xorm:"timestamp updated"`
}
type _inData struct {
Title string `json:"title" form:"title"`
TagList []model.MapTag `json:"tag_list" form:"tag_list"`
}
inData := new(_inData)
c.BodyParser(inData)
fmt.Print(inData)
结果是&{title.value []}
,为什么不能绑定到TagList??
我发送了,json这样的
{"title" : "title.value", "tag_list":[{"id":"1", "tag_name" :"test"},{"id":"2","tag_name":"test2"}]}
我浪费了很多时间,请帮助
同样,我编写了如下代码,
fmt.Println(string(c.Body()))
type _inData struct {
Title string
TagList []model.MapTag `form:"tag_list"`
}
inData := &_inData{}
if err := c.BodyParser(inData); err != nil {
return err
}
fmt.Println(inData)
我得到如下结果,仍然没有绑定到 tagList
{"title":"123123","tag_list":[{"id":42,"tag_name":"234234"},
{"id":43,"tag_name":"345345"}]}
&{123123 []}
我正在将数据从颤振发送到 golang 光纤,如下所示,它对解决问题有帮助吗?
var response = await dio.post(
Uri.parse(System.baseV1 + "/v1/map/create").toString(),
data: {
"title": mapTitle,
"tag_list": tagList.toList(),
// "file": await MultipartFile.fromFile(mainImage!.path),
},
options: Options(
headers: {
"Authorization": "Barer " + storage.getString('token').toString(),
"Content-Type": "application/json"
},
),
);