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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how can i pass http request headers through metadata? #2685

Open
hhniao opened this issue Dec 28, 2023 · 0 comments
Open

how can i pass http request headers through metadata? #2685

hhniao opened this issue Dec 28, 2023 · 0 comments

Comments

@hhniao
Copy link

hhniao commented Dec 28, 2023

i trid this, it is not work,
for example, i wanna pass Accept-Language or Language or any other header to service.
it is cant pass.

err = conn.Invoke(callCtx(c), "/"+s.GrpcCall.gw.Service+"."+s.GrpcCall.gw.Endpoint, request, response)
func callCtx(c *gin.Context) context.Context {
	r := c.Request
	// create context
	myContext := ctx.FromRequest(r)
	// get context from http handler wrappers
	md, ok := metadata.FromContext(r.Context())
	if !ok {
		md = make(metadata.Metadata)
	}
	// fill contex with http headers
	md["Host"] = r.Host
	md["Method"] = r.Method
	// get canonical headers
	for k := range r.Header {
		// may be need to get all values for key like r.Header.Values() provide in go 1.14
		md[textproto.CanonicalMIMEHeaderKey(k)] = r.Header.Get(k)
	}

	// merge context with overwrite
	myContext = metadata.MergeContext(myContext, md, true)
	return myContext
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant