Skip to content

🐛 [Bug]: c.Redirect doesn't respect c.Method #3405

Open
@andradei

Description

@andradei

Bug Description

Calling c.Method("GET") before c.Redirect("/my/route") doesn't set the method and c.Redirect still uses the method that the handler was originally called with.

How to Reproduce

Steps to reproduce the behavior:

  1. Create route app.Put("/put", putHandler)
  2. Create route app.Get("/get", getHandler)
  3. At the bottom of putHandler have c.Method("GET"); return c.Redirect("/get")
  4. Call /put with "PUT" HTTP method/verb
  5. Notice the redirect is not GET /get but PUT /get despite method being set in item 3 above

Expected Behavior

c.Method("GET") is respected and overrides the method/verb of the original request (PUT, in this example).

Fiber Version

v2.52.6

Code Snippet (optional)

package main

import "github.com/gofiber/fiber/v2"
import "log"

func main() {
  app := fiber.New()
  app.Get("/get", getHandler)
  app.Put("/put", putHandler)

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

// This will NEVER be called
func getHandler(c *fiber.Ctx) error {
  c.SendString("")
}

func putHandler(c *fiber.Ctx) error {
  log.Debugf("got here and method it PUT: %v", c.Method())

  c.Method("GET")
  log.Debugf("now the method is set to GET: %v", c.Method())

  // This results in a "PUT /get" request instead of "GET /get"
  c.Redirect("/get")
}

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my problem prior to opening this one.
  • I understand that improperly formatted bug reports may be closed without explanation.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions