Chi: cannot use r (type *http.Request) as type "github.com/cescoferraro/api/vendor/golang.org/x/net/context".Context in argument to chi.URLParam:

Created on 11 Nov 2016  路  5Comments  路  Source: go-chi/chi

func MyRequestHandler(w http.ResponseWriter, r *http.Request) {
    userID := chi.URLParam(r.Context(), "userID") // from a route like /users/:userID
    w.Write([]byte(fmt.Sprintf("hi %v", userID)))
}
func runServer() {
    venom.PrintViperConfig()
    r := chi.NewRouter()
    r.Use(middleware.Logger)
    r.Get("/:userID", MyRequestHandler)
    http.ListenAndServe("0.0.0.0:" + strconv.Itoa(viper.GetInt("port")), r)
}
[00] 2016/11/11 22:28:50 http: panic serving 127.0.0.1:60547: interface conversion: interface {} is nil, not *chi.Context
[00] goroutine 8 [running]:
[00] net/http.(*conn).serve.func1(0xc420086b80)
[00]    /usr/local/go/src/net/http/server.go:1491 +0x12a
[00] panic(0x80aac0, 0xc420013440)
[00]    /usr/local/go/src/runtime/panic.go:458 +0x243
[00] github.com/cescoferraro/api/vendor/github.com/pressly/chi.RouteContext(0x7f9a3dc81190, 0xc420013300, 0x40d02f)
[00]    /go/src/github.com/cescoferraro/api/vendor/github.com/pressly/chi/chi.go:70 +0xed
[00] github.com/cescoferraro/api/vendor/github.com/pressly/chi.URLParam(0x7f9a3dc81190, 0xc420013300, 0x876383, 0x6, 0xc420013300, 0x1)
[00]    /go/src/github.com/cescoferraro/api/vendor/github.com/pressly/chi/chi.go:77 +0x39
[00] github.com/cescoferraro/api/cmd.MyRequestHandler(0x7f9a3dc81158, 0xc420013400, 0xc4200c23c0)
[00]    /go/src/github.com/cescoferraro/api/cmd/server.go:40 +0x84

What am I missing here? ON the README it says userID := chi.URLParam(r, "userID") // from a route like /users/:userID but URLParams signature does not like this func URLParam(ctx context.Context, key string) string {

Most helpful comment

probably because your vendored x/net/context is either behind the 1.7 one or ahead (with brad's new 1.8 cancelfunc changes). if you unvendor it should work.

All 5 comments

Hey, @cescoferraro. The chi.URLParam function receives a *http.Request as parameter but you are passing a context.Context.

To fix your code, use

chi.URLParam(r, "userID")

@ustrajunior I did not made myself clear

with chi.URLParam(r, "userID") I get

cannot use r (type *http.Request) as type "github.com/cescoferraro/api/vendor/golang.org/x/net/context".Context in argument to chi.URLParam

with userID := chi.URLParam(r.Context(), "userID")I get

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

should I be tracking v2?
I thinnk thats it
https://github.com/pressly/chi/blob/v2.0.0rc1/context.go#L60

probably because your vendored x/net/context is either behind the 1.7 one or ahead (with brad's new 1.8 cancelfunc changes). if you unvendor it should work.

Having the same described problem here, removing my x/net/context did not solve the issue. My solution was as @cescoferraro mentioned, pinning my vendor to v2.0.0rc1 _(just fyi for future readings)_

hey guys, yea, @pxue called it. It's the mix up between x/net/context and context which are not compatible with eachother, they are technically different types. Perhaps in 1.9 when they figure out aliasing this will get smoothed out.. that is a while from now. I bet there would be a way to make some pkg that allowed you to use both because it would translate between the types, perhaps I'll write that one day.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hmgle picture hmgle  路  7Comments

kevinconway picture kevinconway  路  8Comments

rocanion picture rocanion  路  4Comments

mvrlin picture mvrlin  路  6Comments

netsharec picture netsharec  路  6Comments