Access metadata in StreamInterceptor response
- Dominant language
- JavaScript
- Stars
- 9.3k
- Forks
- 802
- Avg merge
- 1d 7h
- Merged PRs (30d)
- 5
Description
I've looked through the existing tickets and am still lost on how to access the response headers in a streamInterceptor. I need to update a JWT token if an Authorization header is set in a stream response.
My code uses both unary and streaming calls, and unary calls are working successfully with promise based interceptor and promise-base client with reading and setting headers in both request and response. Based on #988 stream interceptors are only meant for a normal callback-based client and interceptor. Using #766 I implemented a stream interceptor and in my golang server side code, I tried setting the metadata header as a test and sending the stream :
```
header := metadata.Pairs("Authorization", "testing auth header")
stream.SendHeader(header)
err = stream.Send(row)
```
By inspecting the http response, I see that the header is set, however the 'metadata' event is never intercepted and 'data' returns my proto response, but I have no access to the header / metadata.
```
InterceptedStream.prototype.on = function(eventType, callback) {
console.log("event type", eventType)
if (eventType === 'data') {
const newCallback = (response) => {
console.log("stream interceptor response", response) //possible to access metadata here?
// const auth = response.getMetadata().authorization //does not exist
callback(response);
};
this.stream.on(eventType, newCallback);
} else if (eventType === 'metadata') {
const metaCallback = (response) => {
console.log("metadata stream", response) //or I can access metadata here but this is never called
callback(response);
};
this.stream.on(eventType, metaCallback);
} else {
this.stream.on(eventType, callback);
}
return this;
};
return new InterceptedStream(invoker(request));
```
I've noticed that I have access to a promise based client in my autogenerated files for the stream, but I'm not sure how that would be integrated with a stream interceptor, or if I need to use a unary interceptor with the stream in some way.
options are defined as such
```
let options = {
'unaryInterceptors': [new UnaryInterceptor()],
'streamInterceptors': [new StreamInterceptor()]
};
Vue.prototype.$interceptOptions = options
```
and I link my streaming call as such:
```
this.machines = new MachineClient(grpcApiUrl(), null, this.$interceptOptions);
```
I'm using grpc-web v.1.2.1
Contributor guide
Assessment
This issue has not been assessed yet.