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

4

7

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?

Kyanite answered 1/8, 2021 at 23:36 Comment(0)
M
3

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"] // ""
    // ...
})

Musick answered 18/10, 2023 at 4:38 Comment(0)
N
14

You could simply use

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

as mentioned here

Neoteric answered 25/4, 2022 at 7:4 Comment(1)
This is no longer working! Check the correct answer at https://mcmap.net/q/1392288/-how-do-i-get-the-querystring-using-golang-39-s-fiberMusick
B
13

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"))
}

Benthos answered 2/8, 2021 at 0:54 Comment(0)
M
3

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"] // ""
    // ...
})

Musick answered 18/10, 2023 at 4:38 Comment(0)
M
0

What I want is a=b&b=x&d=e as a string

Answering as the accepted one is outdated -- No need to dip into fasthttp any more, and can now simply use c.OriginalURL() to get back "/search?q=something&and=more". See https://docs.gofiber.io/api/ctx/#originalurl

// GET http://example.com/search?q=something&and=more

app.Get("/", func(c *fiber.Ctx) error {
   c.OriginalURL() // "/search?q=something&and=more"

  // ...
})
Musick answered 29/10, 2023 at 13:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.