grpc / grpc-web

gRPC for Web Clients
https://grpc.io
Apache License 2.0
8.56k stars 762 forks source link

Can't get custom headers #1212

Closed allsilaevex closed 2 years ago

allsilaevex commented 2 years ago

I have the following web client code (ts)

import { Request, UnaryResponse, UnaryInterceptor } from 'grpc-web';

type MyInterceptorRequest = Request<MyRequest, MyResponse>;
type MyInterceptorUnaryResponse = UnaryResponse<MyRequest, MyResponse>;

class MyInterceptor implements UnaryInterceptor<MyRequest, MyResponse>  {
    intercept(request: MyInterceptorRequest, invoker: (request: MyInterceptorRequest) => Promise<MyInterceptorUnaryResponse>) {
        console.log('[interceptor] before', request.getRequestMessage());

        return invoker(request).then((response: MyInterceptorUnaryResponse) => {
            console.log('[interceptor] after', response.getMetadata(), response.getStatus() ? response.getStatus().metadata : null);
            return response;
        });
    };
}

const myAPIPromiseClient = new MyAPIPromiseClient(String(process.env.API_HOST), null, { unaryInterceptors: [new MyInterceptor()] });

(async () => {
    try {
        const response = await myAPIPromiseClient.myProcedure(new MyRequest(), undefined);
        console.log(response);
    } catch (err) {
        console.error(err);
    }
})();

and the following response from the server (php swoole) with my custom header

image

But I can't get that header, only two standard ones are returned

image

What am I doing wrong?

allsilaevex commented 2 years ago

I found the problem. It is necessary to add an Access-Control-Expose-Headers header with a list of header names that must appeared in the metadata.