Reputation: 83
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?
Upvotes: 7
Views: 24443
Reputation: 391
You could simply use
app.Get("/", func(c *fiber.Ctx) {
queryValue := c.Query("param_key")
})
as mentioned here
New document is https://docs.gofiber.io/api/ctx/#query
And this method is still supported
Upvotes: 14
Reputation: 23084
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"
// ...
})
Upvotes: 0
Reputation: 23084
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"] // ""
// ...
})
Upvotes: 3
Reputation: 4580
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"))
}
Upvotes: 13