Fiber处理程序接受了错误的内容类型的请求。

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

Fiber handler accepting requests for the wrong content type

问题

我正在使用Fiber 2.44.0Go 1.20构建一个简单的RESTful API。我目前正在测试是否可以强制给定处理程序的可接受内容(这是非常常见的),但看起来我做错了什么,或者fiber.Ctx.Accepts的工作方式不是我期望的那样。

这是我定义的一个非常简单的路由。请注意ctx.Accepts的用法...但无论我使用什么Accept HTTP头,所有请求都会通过。

// app.go
func main() {
	config := fiber.Config{
		StrictRouting: true,
	}
	app := fiber.New(config)
	app.Use(logger.New(), recover.New())

	app.Get("/", handlers.RootHandler)

	log.Fatal(app.Listen(":8080"))
}

// ============================================================================

// handlers/handlers.go
func RootHandler(ctx *fiber.Ctx) error {
	ctx.Accepts(fiber.MIMETextPlain, fiber.MIMETextPlainCharsetUTF8)
	ctx.Set(fiber.HeaderContentType, ctx.GetReqHeaders()[fiber.HeaderAccept])
	return ctx.Status(fiber.StatusOK).SendString("Hello, world!")
}

有没有办法避免这种行为并正确配置处理程序/路由?

英文:

I'm building a simple RESTful API using Fiber 2.44.0 and Go 1.20. I'm currently testing if I can enforce the acceptable content for a given handler (something very common), but it looks like I'm doing something wrong, or fiber.Ctx.Accepts doesn't quite work the way I'm expecting.

This is a very simple route I have defined. Notice the usage of ctx.Accepts...but it doesn't really matter what Accept HTTP header I use, all requests go through.

// app.go
func main() {
	config := fiber.Config{
		StrictRouting: true,
	}
	app := fiber.New(config)
	app.Use(logger.New(), recover.New())

	app.Get("/", handlers.RootHandler)

	log.Fatal(app.Listen(":8080"))
}

// ============================================================================

// handlers/handlers.go
func RootHandler(ctx *fiber.Ctx) error {
	ctx.Accepts(fiber.MIMETextPlain, fiber.MIMETextPlainCharsetUTF8)
	ctx.Set(fiber.HeaderContentType, ctx.GetReqHeaders()[fiber.HeaderAccept])
	return ctx.Status(fiber.StatusOK).SendString("Hello, world!")
}

Is there a way to avoid such behavior and configure the handlers/routes properly?

答案1

得分: 2

func的签名是:

func (c *Ctx) Accepts(offers ...string) string

它将返回从传递给此函数的offers中可接受的offer。如果请求中的Accept头与任何一个offer都不匹配,则返回一个空字符串(请参阅实现)。因此,如果您想强制执行给定处理程序的可接受内容,只需检查返回值是否为空字符串:

func RootHandler(ctx *fiber.Ctx) error {
	if offer := ctx.Accepts(fiber.MIMETextPlain, fiber.MIMETextPlainCharsetUTF8); offer == "" {
		return ctx.SendStatus(fiber.StatusNotAcceptable)
	}
	// ...
}
英文:

The signature of the func is:

func (c *Ctx) Accepts(offers ...string) string

It will return the acceptable offer from the offers you pass into this func. If the Accept header in the request does not match any one of the offers, it returns an empty string (see the implementation). So if you want to enforce the acceptable content for a given handler, you can simply check whether the return value is an empty string:

func RootHandler(ctx *fiber.Ctx) error {
	if offer := ctx.Accepts(fiber.MIMETextPlain, fiber.MIMETextPlainCharsetUTF8); offer == "" {
		return ctx.SendStatus(fiber.StatusNotAcceptable)
	}
	// ...
}

huangapple
  • 本文由 发表于 2023年4月30日 10:42:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76139324.html
匿名

发表评论

匿名网友

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

确定