Skip to content

added additional info for custom http headers #1035

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

Merged
merged 3 commits into from
Sep 9, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions docs/_docs/customizingyourgateway.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,39 @@ You might not like [the default mapping rule](http://godoc.org/github.com/grpc-e
1. Write a [`HeaderMatcherFunc`](http://godoc.org/github.com/grpc-ecosystem/grpc-gateway/runtime#HeaderMatcherFunc).
2. Register the function with [`WithIncomingHeaderMatcher`](http://godoc.org/github.com/grpc-ecosystem/grpc-gateway/runtime#WithIncomingHeaderMatcher)

e.g.
```go
func yourMatcher(headerName string) (mdName string, ok bool) {
...
}
...
mux := runtime.NewServeMux(runtime.WithIncomingHeaderMatcher(yourMatcher))
e.g.
```go
func CustomMatcher(key string) (string, bool) {
switch key {
case "x-custom-header1":
return key, true
case "x-custom-header2":
return "custom-header2", true
default:
return key, false
}
}
...

```
mux := runtime.NewServeMux(runtime.WithIncomingHeaderMatcher(CustomMatcher))

```

## Mapping from gRPC server metadata to HTTP response headers
ditto. Use [`WithOutgoingHeaderMatcher`](http://godoc.org/github.com/grpc-ecosystem/grpc-gateway/runtime#WithOutgoingHeaderMatcher)
ditto. Use [`WithOutgoingHeaderMatcher`](http://godoc.org/github.com/grpc-ecosystem/grpc-gateway/runtime#WithOutgoingHeaderMatcher).
See [gRPC metadata docs](https://github.com/grpc/grpc-go/blob/master/Documentation/grpc-metadata.md)
for more info on sending / receiving gRPC metadata.

e.g.

```go
...
if appendCustomHeader {
grpc.SendHeader(ctx, metadata.New(map[string]string{
"x-custom-header1": "value",
}))
}
```

## Mutate response messages or set response headers
You might want to return a subset of response fields as HTTP response headers;
Expand Down