grpc-web icon indicating copy to clipboard operation
grpc-web copied to clipboard

How to get header value from Streaming.

Open singerxt opened this issue 4 years ago • 1 comments

Hello!

I do have a simple endpoint that streams objects from the database. In headers, I'm sending extra header with information about the total amount of items. The header is called total_count

Zrzut ekranu 2021-10-26 o 00 37 59

I'm seeing the value in the response header in the network debugger. However, I'm unable to get it using your framework.

const headers = new grpc.Metadata();
headers.set('authorization', login?.accessToken || '');
const response = client.listDemoObjects(req, headers);
const items: DemoObject.AsObject[] = [];
let totalItems: number;

response.on('status', (status) => {
  console.log(status.code);
  console.log(status.details);
  console.log(status.metadata); // total_count is not here
});

response.on('end', (status) => {
  console.log(status); // total_count is not here
});

response.on('data', (data) => {
  items.push(data.toObject()); // total_count is not here
});

How can I get response headers?

To be 100% clear I'm not using trailers. :-)

singerxt avatar Oct 25 '21 22:10 singerxt

As noted here you should be able to read the response headers in the "metadata" event. The GrpcWebClientReadableStream implementation directly reads the response headers from the xhr event and will emit the corresponding event.

lukasmoellerch avatar Nov 11 '21 12:11 lukasmoellerch

Hi @lukasmoellerch why do you think the "metadata" doesn't show the other values that were set on the trailers?

This is from the response headers Screenshot from 2022-11-21 15-42-05

While this is from the console when I tried to log the "metadata" Screenshot from 2022-11-21 15-42-52

And this is my code stream.on('metadata', (metadata) => { console.log('metadata', metadata); });

mconrejas avatar Nov 21 '22 07:11 mconrejas

I'd assume that status and message have special handling. Does it not appear anywhere in the response? Maybe in the status event?

lukasmoellerch avatar Nov 21 '22 07:11 lukasmoellerch

Already tried to log the status @lukasmoellerch but the metadata there doesn't contain anything.

This is what the status contains, there should be more values than what's in this picture but for some reason it only shows the values below. image (2)

mconrejas avatar Nov 21 '22 08:11 mconrejas

How about the error event? I am only on mobile right now, can investigate more this evening.

lukasmoellerch avatar Nov 21 '22 08:11 lukasmoellerch

Same goes for the error event, only shows code, details, and an empty metadata.

Could it be because I'm using it on ReactJS + Typescript? Since from our back-end dev's side it does show all values and he's just using plain javascript. Screenshot from 2022-11-21 18-38-09

mconrejas avatar Nov 21 '22 10:11 mconrejas

Hello!

You have to add an additional header in the envoy server so that grpc lib will be able to use it. In my case it was.

expose_headers: grpc-status,grpc-message,total_items

I'm closing this issue as not an issue.

singerxt avatar Nov 23 '22 18:11 singerxt