'How do I get the querystring using Golang's Fiber?

My code

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

                // GET THE ACCOUNT FROM THE PATH
                fmt.Println("PATH")
                fmt.Println(c.Path())
                fmt.Println("END PATH")
                fmt.Printf("%+v\n", c)
                fmt.Println("END PERCENT V")
                msg := fmt.Sprintf("%s\n", c.Params("*"))
                fmt.Println(msg)
                fmt.Println("END PARAMS *")
                fmt.Println(c.Body())
                fmt.Println("END BODY")
                fmt.Println(c)
                fmt.Println("END RAW")

I call this

curl 'localhost:3000/event/?a=b&b=c&d=e'

My output

PATH
/event/
END PATH
#0000000100000001 - 127.0.0.1:3000 <-> 127.0.0.1:46890 - GET http://localhost:3000/event/?a=b&b=c&d=e
END PERCENT V


END PARAMS *
[]
END BODY
#0000000100000001 - 127.0.0.1:3000 <-> 127.0.0.1:46890 - GET http://localhost:3000/event/?a=b&b=c&d=e
END RAW

What I want is a=b&b=x&d=e as a string or as a json object. However! There is no fixed format to this string. it could just as easily be blah=123&blah2=234. or x=1&somekey=somevalue

All the docs i can find involve turning the querystring into a struct. All this service needs to do is convert the query string to json and write it to disk. If I drop the ?, I can use Params('*') to get it, but then the path is problematic, because I need the word "/event/" also. And that value is also arbitrary. This service just writes it to disk and return 200.

But I cannot figure out how to get the query string using golang's Fiber. Is there a way? If not, is there some other performant method of getting this?



Solution 1:[1]

fiber uses fasthttp.

package main

import (
    "fmt"
    "log"

    "github.com/gofiber/fiber/v2"
)

func main() {
    app := fiber.New()

    app.Get("/", func (c *fiber.Ctx) error {
        fmt.Println(string(c.Request().URI().QueryString()))
        return c.SendString("Hello, World!")
    })

    log.Fatal(app.Listen(":3000"))
}

Solution 2:[2]

You could simply use

app.Get("/", func(c *fiber.Ctx) {
  queryValue := c.Query("param_key")
})

as mentioned here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lucas Katayama
Solution 2 Mojtaba Shayegh