Skip to content

How to access path parameters from HTTP middleware after grpc-gateway ServeMux? #5824

@nhatpy

Description

@nhatpy

Context

I'm using grpc-gateway v2 to expose my gRPC services over HTTP. My setup looks like this:

mux := runtime.NewServeMux()
_ = RegisterServiceHandlerServer(ctx, mux, myService)
r := chi.NewRouter()
r.Use(loggingMiddleware) // Chi middleware
r.Route("/v1/orgs", func(org chi.Router) {
    org.Use(authenticationMiddleware)
    org.Use(authorizationOrgMiddleware)
    org.Mount("/", mux)
})

In authorizationOrgMiddleware, I want to read the org_id path parameter (declared in my .proto as /v1/orgs/{org_id}/...). However, using chi.URLParam doesn't work because the request is handled by the gRPC-Gateway mux before reaching Chi.

I tried:

params := runtime.PathParams(r) // undefined
params := runtime.HTTPRequestPathParams(r.Context()) // undefined

Question
Is there an officially supported way to read gRPC-Gateway path parameters inside an HTTP middleware?

If the helper functions like HTTPRequestPathParams or PathParams are not available in my version, what is the recommended approach?

Would using WithMetadata() when creating the ServeMux be a viable workaround to attach path params or patterns into context for later retrieval?

Thanks for any guidance!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions