如何在Echo框架中绑定multipart/form-data数组?

huangapple go评论101阅读模式
英文:

How to bind multipart/form-data array in Echo framework?

问题

我正在使用Flutter和Go Echo框架编写一个API服务器,我想从Flutter发送数据到Go并保存它,但是在Go中c.bind()不起作用:

type _getData struct {
    Title    string      `json:"title" form:"title"`
    Address  string      `json:"address" form:"address"`
    Location string      `json:"location" form:"location"`
    MapId    uint        `json:"map_id" form:"map_id"`
    Date     _customTime `json:"date" form:"date"`
    Pages    []struct {
        Order       int    `json:"order" form:"order"`
        Description string `json:"description" form:"description"`
    } `json:"pages" form:"pages"`
    Tags []struct {
        TagName string `json:"tag_name" form:"tag_name"`
        ID      string `json:"id" form:"id"`
    } `json:"tags" form:"tags"`
}

type _customTime struct {
    time.Time
}

按照上面的代码创建一个结构体,并按照下面的方式进行绑定:

d := &echo.DefaultBinder{}
var aa _getData
d.Bind(&aa, c)
fmt.Println(c.Request().Form)
fmt.Println(aa)
----- fmt.Println(c.Request().Form)的结果是 -----
ap[address:[미국 캘리포니아 산타클라라 카운티 쿠퍼티노 ] date:[2021-10-01] location: 
[37.330672396748554 -122.03014377504589] pages[0][description]:[123123] pages[0] 
[order]:[0] tags[0][tag_name]:[sdf] title:[123123]]

----- fmt.Println(aa)的结果是 -----
{123123 미국 캘리포니아 산타클라라 카운티 쿠퍼티노  37.330672396748554 -122.03014377504589 0 
2021-10-01 00:00:00 +0900 KST [] []}

pages和tags数据没有被绑定。
其他字段被绑定了,但为什么不是多维数组字段?我是初学者,希望能得到一些建议。

英文:

I'm writing an api server using flutter and Go Echo framework, I want to send data from flutter to Go and save it, but c.bind() doesn't work in Go:

type _getData struct {
	Title    string      `json:"title" form:"title"`
	Address  string      `json:"address" form:"address"`
	Location string      `json:"location" form:"location"`
	MapId    uint        `json:"map_id" form:"map_id"`
	Date     _customTime `json:"date" form:"date"`
	Pages    []struct {
		Order       int    `json:"order" form:"order"`
		Description string `json:"description" form:"description"`
	} `json:"pages" form:"pages"`
	Tags []struct {
		TagName string `json:"tag_name" form:"tag_name"`
		ID      string `json:"id" form:"id"`
	} `json:"tags" form:"tags"`
}

type _customTime struct {
	time.Time
}

Create a structure as in the code above, and bind it as shown below:

d := &echo.DefaultBinder{}
var aa _getData
d.Bind(&aa, c)
fmt.Println(c.Request().Form)
fmt.Println(aa)
----- fmt.Println(c.Request().Form)'s result is -----
ap[address:[미국 캘리포니아 산타클라라 카운티 쿠퍼티노 ] date:[2021-10-01] location: 
[37.330672396748554 -122.03014377504589] pages[0][description]:[123123] pages[0] 
[order]:[0] tags[0][tag_name]:[sdf] title:[123123]]

----- fmt.Println(aa)'s result is -----
{123123 미국 캘리포니아 산타클라라 카운티 쿠퍼티노  37.330672396748554 -122.03014377504589 0 
2021-10-01 00:00:00 +0900 KST [] []}

The pages and tags data are not bound.
Other fields are bound, but why not just the multiarray field? How can I bind? I'm a beginner, any advice would be appreciated.

答案1

得分: 0

Echo框架默认不支持直接从表单数据绑定数组。

你可以使用JSON代替,或者使用第三方库。参考这个实现这个帖子,以及GitHub问题https://github.com/labstack/echo/issues/1644

英文:

Echo framework does not support binding array from form data out of the box.

You can use json instead or use 3rd party library. See implementation or post and github issue https://github.com/labstack/echo/issues/1644

huangapple
  • 本文由 发表于 2021年10月2日 00:16:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/69409036.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定