Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Passing context in *http.request in middleware gives an error

I am trying to pass some data to handler function through a chi middleware like this:

ctx := context.WithValue(context.Background(), int32(0), company)
next.ServeHTTP(w, r.WithContext(ctx))
return

But the next.ServeHTTP() throws this error:

interface conversion: interface {} is nil, not *chi.Context

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

context.Background() gives a non-nil context and that’s why the interface{} is nil error is arising. You need to use update the context embedded in request itself. Try this:

ctx := context.WithValue(r.Context(), int32(0), company)
next.ServeHTTP(w, r.WithContext(ctx))
return
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading