英文:
How do I get the querystring using Golang's Fiber?
问题
我的代码
app.Get("/event/*", func(c *fiber.Ctx) error {
// 从路径中获取账户
fmt.Println("路径")
fmt.Println(c.Path())
fmt.Println("结束路径")
fmt.Printf("%+v\n", c)
fmt.Println("结束百分比V")
msg := fmt.Sprintf("%s\n", c.Params("*"))
fmt.Println(msg)
fmt.Println("结束参数 *")
fmt.Println(c.Body())
fmt.Println("结束正文")
fmt.Println(c)
fmt.Println("结束原始")
我调用了这个
curl 'localhost:3000/event/?a=b&b=c&d=e'
我的输出
路径
/event/
结束路径
#0000000100000001 - 127.0.0.1:3000 <-> 127.0.0.1:46890 - GET http://localhost:3000/event/?a=b&b=c&d=e
结束百分比V
结束参数 *
[]
结束正文
#0000000100000001 - 127.0.0.1:3000 <-> 127.0.0.1:46890 - GET http://localhost:3000/event/?a=b&b=c&d=e
结束原始
我想要的是
a=b&b=x&d=e 作为一个字符串或一个 JSON 对象。然而!这个字符串没有固定的格式。它也可以是 blah=123&blah2=234,或者 x=1&somekey=somevalue。
我找到的所有文档都涉及将查询字符串转换为结构体。这个服务只需要将查询字符串转换为 JSON 并将其写入磁盘。如果我去掉问号,我可以使用 Params('*') 来获取它,但是路径是有问题的,因为我还需要单词 "/event/"。而且该值也是任意的。这个服务只是将其写入磁盘并返回 200。
但是我无法弄清楚如何使用 golang 的 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?
答案1
得分: 11
fiber
使用 fasthttp
。
- https://pkg.go.dev/github.com/gofiber/fiber/v2#Request
- https://pkg.go.dev/github.com/valyala/fasthttp#RequestCtx.URI
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"))
}
请注意,这只是代码的翻译部分,不包括任何其他内容。
英文:
fiber
uses fasthttp
.
- https://pkg.go.dev/github.com/gofiber/fiber/v2#Request
- https://pkg.go.dev/github.com/valyala/fasthttp#RequestCtx.URI
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"))
}
答案2
得分: 11
你可以简单地使用以下代码:
app.Get("/", func(c *fiber.Ctx) {
queryValue := c.Query("param_key")
})
如这里所提到的那样。
英文:
You could simply use
app.Get("/", func(c *fiber.Ctx) {
queryValue := c.Query("param_key")
})
as mentioned here
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论