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

Forward All Headers from Gateway to Service #107

Closed
krisanalfa opened this issue Apr 29, 2020 · 1 comment
Closed

Forward All Headers from Gateway to Service #107

krisanalfa opened this issue Apr 29, 2020 · 1 comment

Comments

@krisanalfa
Copy link

How do I achieve this? I wrote this middleware, turns out the middleware can't read gateway request headers.

forwardHeaderMiddleware := gateway.RequestMiddleware(func(r *http.Request) error {
	// Loop over header names
	for name, values := range r.Header {
		// Loop over all values for the name.
		for _, value := range values {
			r.Header.Set(name, value)
		}
	}

	return nil
})
@krisanalfa
Copy link
Author

Found the answer, the trick is using custom context value

forwardHeaderMiddleware := gateway.RequestMiddleware(func(r *http.Request) error {
	h := r.Context().Value(customContextKey).(http.Header)

	// Loop over header names
	for name, values := range r.Header {
		// Loop over all values for the name.
		for _, value := range values {
			r.Header.Set(name, value)
		}
	}

	return nil
})

http.HandleFunc("/graphql",
	func(w http.ResponseWriter, r *http.Request) {
		r = r.WithContext(context.WithValue(r.Context(), customContextKey, r.Header))

		gw.GraphQLHandler(w, r)
	})

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