【问题标题】:How to bind multipart/form-data array in Echo framework?如何在 Echo 框架中绑定 multipart/form-data 数组?
【发布时间】:2021-10-01 16:16:26
【问题描述】:

我正在使用flutter和Go Echo框架编写一个api服务器,我想将数据从flutter发送到Go并保存,但是c.bind()在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
}

按照上面的代码创建一个结构体,如下图所示绑定:

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 [] []}

页面和标签数据未​​绑定。 其他字段是绑定的,但为什么不只是多数组字段?我该如何绑定?我是初学者,如有任何建议,将不胜感激。

【问题讨论】:

  • c.Request().Header.Get("Content-Type") 返回什么?你发送什么数据?从我所看到的我认为Formdata,那么为什么你在struct上指定json标签?
  • c.Request().Header.Get("Content-Type")的结果如下。多部分/表单数据;边界=--dio-boundary-4032587440 我需要做什么才能获取这些数据?

标签: go go-echo


【解决方案1】:

Echo 框架不支持开箱即用的表单数据绑定数组。

您可以改用 json 或使用 3rd 方库。见implementationpost和github问题https://github.com/labstack/echo/issues/1644

【讨论】:

  • 同时也是可能的。在 Echo v4.6.3 上与 form:"value[]" 一起使用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-21
  • 1970-01-01
  • 1970-01-01
  • 2018-12-01
  • 1970-01-01
  • 2014-10-31
相关资源
最近更新 更多