sam
sam

Reputation: 787

How to access Google Cloud Endpoints request header in Python and Java

In the endpoints method, how to access request header information?

Upvotes: 3

Views: 1328

Answers (2)

Hugues Zimmermann
Hugues Zimmermann

Reputation: 1

What is working for me in python is the following:

The request I use: http://localhost:8080/api/hello/v1/header?message=Hello World!

python code:

    @endpoints.api(name='hello', version='v1', base_path='/api/')
    class EchoApi(remote.Service):
        @endpoints.method(
            # This method takes a ResourceContainer defined above.
            message_types.VoidMessage,
            # This method returns an Echo message.
            EchoResponse,
            path='header',
            http_method='GET',
            name='header')
        def header(self, request):
            header = request._Message__unrecognized_fields
            output_message = header.get(u'message', None)[0]
            return EchoResponse(message=output_message)

Upvotes: 0

sam
sam

Reputation: 787

Python:

In the endpoint method, self.request_state.headers provides this information.

E.g., self.request_state.headers.get('authorization')

Java: Add an HttpServletRequest (req) parameter to your endpoint method. The headers are accessible through the method getHeader() e.g., req.getHeader("Authorization") See this question

Upvotes: 4

Related Questions