Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃 [Question]: how to Get status code in middleware #2928

Open
3 tasks done
tylitianrui opened this issue Mar 20, 2024 · 5 comments
Open
3 tasks done

馃 [Question]: how to Get status code in middleware #2928

tylitianrui opened this issue Mar 20, 2024 · 5 comments

Comments

@tylitianrui
Copy link

Question Description

same Get status code middleware
status code is always 200

Solution does not work

鎴睆2024-03-20 13 58 40

Code Snippet (optional)

package main

import (
	"log"

	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/middleware/recover"
)

func main() {
	app := fiber.New()
	app.Use(
		func(c *fiber.Ctx) error {
			err := c.Next()
			log.Printf("status: %v", c.Response().StatusCode())
			return err
		},
		recover.New(),
	)

	app.Listen(":8520")
}

Checklist:

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

welcome bot commented Mar 20, 2024

Thanks for opening your first issue here! 馃帀 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

@rngallen
Copy link

For that you should use logger middleware it will do that for you Logger

@ReneWerner87
Copy link
Member

not found is a bit special because it is set outside the flow

but you can also set it yourself as described here
https://docs.gofiber.io/extra/faq#how-do-i-handle-custom-404-responses
and then the logging should also work

@tylitianrui
Copy link
Author

not found is a bit special because it is set outside the flow

but you can also set it yourself as described here https://docs.gofiber.io/extra/faq#how-do-i-handle-custom-404-responses and then the logging should also work

i want handling responses according to status codes . so i need status codes in middleware

@ReneWerner87
Copy link
Member

as already mentioned, it should work with the variant as you have currently done it
but only if you set the not found status yourself via middleware, as it is done in the FAQ article

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants