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

huangapple go评论100阅读模式
英文:

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(&quot;/event/*&quot;, func(c *fiber.Ctx) error {

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

I call this

curl &#39;localhost:3000/event/?a=b&amp;b=c&amp;d=e&#39;

My output

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


END PARAMS *
[]
END BODY
#0000000100000001 - 127.0.0.1:3000 &lt;-&gt; 127.0.0.1:46890 - GET http://localhost:3000/event/?a=b&amp;b=c&amp;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

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.

package main

import (
	&quot;fmt&quot;
	&quot;log&quot;

	&quot;github.com/gofiber/fiber/v2&quot;
)

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

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

	log.Fatal(app.Listen(&quot;:3000&quot;))
}

答案2

得分: 11

你可以简单地使用以下代码:

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

这里所提到的那样。

英文:

You could simply use

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

as mentioned here

huangapple
  • 本文由 发表于 2021年8月2日 07:36:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/68615135.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定