pythongoogle-app-engineprotorpc

Get request headers using GAE Protocol RPC service method


I am using Google App Engine's Protocol RPC library. I want to get the headers for a request and check that a certain header exists. I can't figure out how to get the requests headers?

The code basically looks like this:

class MyService(remote.Service):
    @remote.method(MyRequest, MyResponse)
    def my_request(self, request):
        # TODO: Check that header exists in request

The passed in request object is of the type 'MyRequest' and doesn't have any header information attached to it.


Solution

  • There is a special method initialize_request_state that allows you to access all of the requests headers.

    class MyService(remote.Service):
    
        def initialize_request_state(self, state):
            self.headers = state.headers
    
        @remote.method(MyRequest, MyResponse)
        def my_request(self, request):
            logging.debug(self.headers)