0
type _getData struct {
Title string `json:"title" form:"title"`
Date  string `json:"date" form:"date"`
Pages []struct {
    Order       int    `json:"order" form:"title"`
    Description string `json:"description" form:"description"`
} `json:"pages" form:"pages"`


func CreateDiary(c echo.Context) error {

var getData _getData
c.Bind(&getData)
fmt.Print(getData)
...

Receive the following data through c.FormParams command, please tell me how to bind it to _getData struct,

map[address:[미국 캘리포니아 산타클라라 카운티 쿠퍼티노 ] date:[2021-10-05] location:[37.32779072192643 -122.01981157064436] map_id:[0] pages[0][description]:[123123] pages[0][order]:[0] pages[1][description]:[123123] pages[1][order]:[1] tags[0][id]:[12] tags[0][tag_name]:[sdf] title:[123123]]

I want to get the data of pages as an array, but I am getting []

JimB
  • 104,193
  • 13
  • 262
  • 255
Choi yun seok
  • 309
  • 2
  • 15
  • Does this answer your question? [How to bind multipart/form-data array in Echo framework?](https://stackoverflow.com/questions/69409036/how-to-bind-multipart-form-data-array-in-echo-framework) – MenyT Oct 05 '21 at 07:01
  • I know, but I can't get the data of pages – Choi yun seok Oct 05 '21 at 07:54
  • result is {title 2021-10-05 []},,,, empty pages ㅠㅠㅠㅠㅠ – Choi yun seok Oct 05 '21 at 07:54
  • You can use `header application/json` and send the data as json. It works well. There is issue with parsing `multipart/form-data` array of custom type. – MenyT Oct 05 '21 at 07:59
  • flutter sends the photo file through dio, if you change it to json, won't it not be able to receive the photo? – Choi yun seok Oct 05 '21 at 08:07
  • Choi yun seok Please next time ask for more info in comment under answer rather then creating new question for the same problem. – MenyT Oct 05 '21 at 11:54

1 Answers1

0

You can use 3rd party lib.

import "github.com/monoculum/formam/v3"
type MyFormData struct {
    Pages []struct {
        Order       int    `formam:"order"`
        Description string `formam:"description"`
    } `formam:"pages"`
    Tags []struct {
        TagName string `formam:"tag_name"`
        Id      string `formam:"id"`
    } `formam:"tags"`
    Title string `formam:"title"`
}
func HttpHandler(c echo.Context) error {
    myFormData := MyFormData{}
    form, err := c.FormParams()

    if err != nil {
        return err
    }

    dec := formam.NewDecoder(&formam.DecoderOptions{TagName: "formam"})
    dec.Decode(form, &myFormData)

    return c.JSON(200, myFormData)
}
MenyT
  • 1,653
  • 1
  • 8
  • 19