Fiber: 馃殌 add 405 method not allowed handler

Created on 20 Jun 2020  路  6Comments  路  Source: gofiber/fiber

Is your feature request related to a problem?
No

Describe the solution you'd like
Assuming that we have code like:

app := New()

app.Setting.HandleMethodNotAllowed = true

app.Post("/path", func(ctx *Ctx) {
    ctx.Send("hello")
})

app.Listen(3000)

and when we use the wrong method:

curl localhost:3000/path -I
> HTTP/1.1 405 Method Not Allowed
> Date: Sat, 20 Jun 2020 08:54:13 GMT
> Content-Type: text/plain; charset=utf-8
> Content-Length: 18

Additional context

  • This feature will not bring breaking changes
  • More friendlier to users
  • Users can provide custom handler to handle 405
馃彿 Wait for Release

Most helpful comment

We could add a boolean option in Settings named MethodNotAllowed bool and if set to true, instead of sending a 404 it will check the stack if it matches a different method and returns a 405 Method Not Allowed with the correct Allow header https://tools.ietf.org/html/rfc2616#section-10.4.6

All 6 comments

We could add a boolean option in Settings named MethodNotAllowed bool and if set to true, instead of sending a 404 it will check the stack if it matches a different method and returns a 405 Method Not Allowed with the correct Allow header https://tools.ietf.org/html/rfc2616#section-10.4.6

Yes, that鈥檚 my intention:smile:

Fiber will respond with a 405 by default if another method is available on the same path.
This feature will be shipped in v1.12.1 next week, thank you for your suggestion 馃コ

@Fenny I use 1.12.4, but I can't see the 405 response you described, here is my code:

func InitRoute(app *fiber.App) {
    v1 := app.Group("/v1")
    app.Get("/v2", func(ctx *fiber.Ctx) {}) // /v2
    v1.Get("/v2", func(ctx *fiber.Ctx) {}) // /v1/v2
}

app.Get (200)
image

app.Post (405 but 404)
image

v1.Get (200)
image

v1.Post (405 but 404)
image

@Aoi-hosizora, thank you for your detailed bug report. Do you mind creating a separate issue regarding this matter?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

koddr picture koddr  路  4Comments

petersephrin picture petersephrin  路  4Comments

jeyraj picture jeyraj  路  4Comments

Ivan-Feofanov picture Ivan-Feofanov  路  3Comments

mayowa picture mayowa  路  3Comments