Skip to content

Commit 82dc083

Browse files
cemensonachew22
authored andcommitted
added additional info for custom http headers (#1035)
* added additional info for custom http headers
1 parent c53be1d commit 82dc083

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

docs/_docs/customizingyourgateway.md

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,39 @@ You might not like [the default mapping rule](http://godoc.org/github.com/grpc-e
8383
1. Write a [`HeaderMatcherFunc`](http://godoc.org/github.com/grpc-ecosystem/grpc-gateway/runtime#HeaderMatcherFunc).
8484
2. Register the function with [`WithIncomingHeaderMatcher`](http://godoc.org/github.com/grpc-ecosystem/grpc-gateway/runtime#WithIncomingHeaderMatcher)
8585

86-
e.g.
87-
```go
88-
func yourMatcher(headerName string) (mdName string, ok bool) {
89-
...
90-
}
91-
...
92-
mux := runtime.NewServeMux(runtime.WithIncomingHeaderMatcher(yourMatcher))
86+
e.g.
87+
```go
88+
func CustomMatcher(key string) (string, bool) {
89+
switch key {
90+
case "x-custom-header1":
91+
return key, true
92+
case "x-custom-header2":
93+
return "custom-header2", true
94+
default:
95+
return key, false
96+
}
97+
}
98+
...
9399

94-
```
100+
mux := runtime.NewServeMux(runtime.WithIncomingHeaderMatcher(CustomMatcher))
101+
102+
```
95103

96104
## Mapping from gRPC server metadata to HTTP response headers
97-
ditto. Use [`WithOutgoingHeaderMatcher`](http://godoc.org/github.com/grpc-ecosystem/grpc-gateway/runtime#WithOutgoingHeaderMatcher)
105+
ditto. Use [`WithOutgoingHeaderMatcher`](http://godoc.org/github.com/grpc-ecosystem/grpc-gateway/runtime#WithOutgoingHeaderMatcher).
106+
See [gRPC metadata docs](https://github.com/grpc/grpc-go/blob/master/Documentation/grpc-metadata.md)
107+
for more info on sending / receiving gRPC metadata.
108+
109+
e.g.
110+
111+
```go
112+
...
113+
if appendCustomHeader {
114+
grpc.SendHeader(ctx, metadata.New(map[string]string{
115+
"x-custom-header1": "value",
116+
}))
117+
}
118+
```
98119

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

0 commit comments

Comments
 (0)