Sending HTTP headers in calls from gRPC PHP client

491 views
Skip to first unread message

tanm...@gmail.com

unread,
Sep 10, 2018, 12:40:53 PM9/10/18
to grpc.io

I am attempting to set some HTTP headers in gRPC client call written in PHP. I cannot find anything specifying how to do this in PHP. From reading docs for other languages I have come to think that headers are specified in the client metadata. However, I can't find anything on how these should be formatted, and all the formats I try don't seem to work. Here is my code:


        $options = [
            'credentials' => $this->credentialsObject,
            'update_metadata' => function($metaData){
                $metaData['headers'] = ['Authorization' => 'Bearer ' . $this->token];
                return $metaData;
            }
        ];
        
        $client = new OrganizationServiceClient($this->url,$options);
        
        $r = new \Google\Protobuf\GPBEmpty();

        list($data,$status) = $client->list($r)->wait();


Any help or pointers would be appreciated. I'm been working on this for a few days now and feel like I've trying everything I can think of.

Thanks!

Carl Mastrangelo

unread,
Sep 10, 2018, 1:39:08 PM9/10/18
to grpc.io
In gRPC, Headers and metadata are the same thing.   The formatting for headers should follow normal HTTP rules, such as ignoring case for the header key, and only using the right ascii letters in the value.  You can find the rules here: https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md     

tanm...@gmail.com

unread,
Sep 10, 2018, 2:10:24 PM9/10/18
to grpc.io
Thank you Carl Mastrangelo. That was very helpful!

For those who may see this question in the future below is my updated code that works. Notice how setting the $metaData array uses the HTTP2 header key as the array key, and the header value is and array containing a string of the value.

        $options = [
            'credentials' => $this->credentialsObject,
            'update_metadata' => function($metaData){
                $metaData['authorization'] = ['Bearer ' . $this->token];
                return $metaData;
            }
        ];
        
        $client = new OrganizationServiceClient($this->url,$options);
        
        $r = new \Google\Protobuf\GPBEmpty();

        list($data,$status) = $client->list($r)->wait();


Reply all
Reply to author
Forward
0 new messages