grpc / grpc-dart

The Dart language implementation of gRPC.
https://pub.dev/packages/grpc
Apache License 2.0
835 stars 256 forks source link

Cannot read HTTP headers of client response #683

Closed spreeni closed 6 months ago

spreeni commented 6 months ago

I can't find a way to parse HTTP headers of a grpc client response.

I am trying to connect to an API which answers with a set-cookie header that I need to parse.

grpc-dart version: 3.2.4

Repro steps

  1. Send a request from a grpc client
  2. Examine the response, it just contains the deserialized data and none of the header information
  3. This is the same in a ClientInterceptor (e.g. https://stackoverflow.com/questions/72949714/how-to-intercept-grpc-response/72949742#72949742)

Expected result: I can somehow access header information of the response

Actual result: The response just contains the deserialized data and none of the header information

I am a Dart/gRPC newbie, so I might not be familiar with a possible solution here, but am open to any suggestions.

mraleph commented 6 months ago

Is this native gRPC (i.e. in the mobile app) or a gRPC-Web (i.e. in the browser)?

In the browser you will not be able to access Set-Cookie because it is a forbidden response header and will be hidden from the client code.

(The browser manages cookie itself - so normally you should not need to access Set-Cookie, e.g. if the cookie is added by the response it will be used by subsequent requests automatically).

spreeni commented 6 months ago

Ah sorry - this is native gRPC, I am trying to add a mobile app to a previous Web implementation, hence the issue.

So is it better to avoid accessing the set-cookie header all together and push for a mobile-adjustment of the backend API? This is possible, but obviously I'd rather work with the existing API, if feasible.

Thanks for the quick reply!

mraleph commented 6 months ago

On the native side I would have expected all headers to be passed back to the client without any filtering. Does response.headers not contain cookie header?

spreeni commented 6 months ago

Maybe I am missing something, but the deserialized response seems to not have a headers attribute. It implements the abstract class protobuf.GeneratedMessage and just contains a _fieldSet.

image

mraleph commented 6 months ago

I am talking about Response.headers property.

You need to try something like:

final call = client.someMethod(...);
final headers = await call.headers;
final response = await call; 

to get access to them.

spreeni commented 6 months ago

Oh thanks for the solution! I was just executing

final response = await client.someMethod(...);

directly and skipping the header retrieval completely. 🙈 Thanks for the quick and very helpful answers, closing this issue again then.