http.Request and Context

Hi there,

I am pretty new to Go and I need to write a web application. So far so good, everything works fine. I added a middleware to authenticate the users via jwt - Also fine.

Now I want to make the user available for http handlers. For this my solution is this:

req := r.WithContext(context.WithValue(context.Background(), "user", user))
// call the next handler with new request object...

r is the original request object
Is this an appropriate way or could/should it be solved in a different way?

Thanks in advance

Nope, that’s how it works.

Alright - Thank you for your answer!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.