gofiber / fiber

⚡️ Express inspired web framework written in Go

Home Page:https://gofiber.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

🤗 [Question]: Parsing `map` in POST `form`

solerf opened this issue · comments

Question Description

I have the following struct (snippet) that contains a map inside but I can not make it to be parsed, in the println I get the string attribute populated but the map empty. The data is being sent as form POST to the handler.

The data is posted as:

a: some_value
theMap[X]: 3
theMap[Y]: 2

Does fiber supports this kind of parsing or I need to customise it? Thanks

Code Snippet (optional)

package main

import "github.com/gofiber/fiber/v3"
import "fmt"

type MyData struct {
  A      string            `json:"a" form:"a"`
  TheMap map[string]string `json:"theMap" form:"theMap"`
}

// on the handler
func theHandler(c *fiber.Ctx) error {
  var data MyData
  _ = c.BodyParser(&data)
  fmt.Printf("at route -- %v\n", data)

  return nil
}

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my questions prior to opening this one.
  • I understand that improperly formatted questions may be closed without explanation.

Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

Question Description

I have the following struct (snippet) that contains a map inside but I can not make it to be parsed, in the println I get the string attribute populated but the map empty. The data is being sent as form POST to the handler.

The data is posted as:

a: some_value
theMap[X]: 3
theMap[Y]: 2

Does fiber supports this kind of parsing or I need to customise it? Thanks

Code Snippet (optional)

package main

import "github.com/gofiber/fiber/v3"
import "fmt"

type MyData struct {
  A      string            `json:"a" form:"a"`
  TheMap map[string]string `json:"theMap" form:"theMap"`
}

// on the handler
func theHandler(c *fiber.Ctx) error {
  var data MyData
  _ = c.BodyParser(&data)
  fmt.Printf("at route -- %v\n", data)

  return nil
}

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my questions prior to opening this one.
  • I understand that improperly formatted questions may be closed without explanation.

You might need a streamlined way to parse form data into a struct with a map field in Fiber.

Example:

package main

import (
    "github.com/gofiber/fiber/v3"
    "log"
)

type MyData struct {
    A      string            `json:"a" form:"a"`
    TheMap map[string]string `json:"theMap"`
}

func theHandler(c *fiber.Ctx) error {
    // Parse the non-map data using BodyParser
    var data MyData
    if err := c.BodyParser(&data); err != nil {
        return err
    }

    // Initialize the map
    data.TheMap = make(map[string]string)

    // Parse the map data manually
    c.Request().PostArgs().VisitAll(func(key, value []byte) {
        // Assuming form keys for the map are prefixed with "theMap["
        if skey := string(key); skey[:6] == "theMap" {
            mapKey := skey[7 : len(skey)-1] // Extract the key inside "theMap[]"
            data.TheMap[mapKey] = string(value)
        }
    })

    log.Printf("at route -- %+v\n", data)

    return nil
}

yeah, I did something similar to it. Appreciated @H0llyW00dzZ 👍