0

This is what I'm doing with Get requests

app.Get("/", func(c *fiber.Ctx) error {


    fmt.Println(string(c.Request().URI().QueryString()))
    
    return c.SendString("Ok!")
  })

I'm getting the following output

hello=12&sdsdf=324

I want to do the same for POST request. I tried string(c.Body()) but I got output like the following

----------------------------948304762891896410291124
Content-Disposition: form-data; name="hello"

ge
----------------------------948304762891896410291124
Content-Disposition: form-data; name="ge"

dsfs
----------------------------948304762891896410291124--

How can I get a query string format out put for POST. The post parameters can be anything and any number.

AH.
  • 741
  • 1
  • 12
  • 27

1 Answers1

3

You could try BodyParser method, the Document Link at https://docs.gofiber.io/api/ctx#bodyparser


type Person struct {
    Name string `json:"name" xml:"name" form:"name"`
}

app.Post("/", func(c *fiber.Ctx) error {
        p := new(Person)
        if err := c.BodyParser(p); err != nil {
            return err
        }
        log.Println(p.Name)
        return nil
})
hienduyph
  • 94
  • 1
  • 4
  • In this solution, I should know the list of parameters which is going to come, right? The server doesn't know what all params will be coming. – AH. Apr 19 '22 at 14:06
  • Yes, BodyParser requires a struct to parse the body, if you want all params, checkout the MultiPartForm at https://docs.gofiber.io/api/ctx#multipartform – hienduyph Apr 22 '22 at 02:55