gofibers

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

  • Answering as the top voted answer is wrong -- no longer working now, and the wiki link is outdated.

    The newest doc is here. Here is sample code:

    // GET http://example.com/?name=alex&want_pizza=false&id=
    
    app.Get("/", func(c *fiber.Ctx) error {
        m := c.Queries()
        m["name"] // "alex"
        m["want_pizza"] // "false"
        m["id"] // ""
        // ...
    })