0

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.

blackgreen
  • 34,072
  • 23
  • 111
  • 129
Choi yun seok
  • 309
  • 2
  • 15
  • What `c.Request().Header.Get("Content-Type")` returns? And what data are you sending? From what I see I think Formdata, so why you specify json tag on struct? – MenyT Oct 02 '21 at 08:14
  • The result of c.Request().Header.Get("Content-Type") is as follows. multipart/form-data; boundary=--dio-boundary-4032587440 What do I need to do to get this data? – Choi yun seok Oct 04 '21 at 04:06

1 Answers1

0

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

MenyT
  • 1,653
  • 1
  • 8
  • 19