如何在Python和Java中访问Google Cloud Endpoints请求标头

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在Python和Java中访问Google Cloud Endpoints请求标头相关的知识,希望对你有一定的参考价值。

在端点方法中,如何访问请求头信息?

答案

蟒蛇:

在端点方法中,self.request_state.headers提供此信息。

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

Java:向端点方法添加HttpServletRequest(req)参数。可以通过getHeader()方法访问标题,例如,req.getHeader("Authorization")查看此question

另一答案

在python中为我工作的是以下内容:

我使用的请求:http:// localhost:8080 / api / hello / v1 / header?message = Hello World!

python代码:

    @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)

以上是关于如何在Python和Java中访问Google Cloud Endpoints请求标头的主要内容,如果未能解决你的问题,请参考以下文章