【问题标题】:Getting raw HTTP Data (Headers, Cookies, etc) in Google Cloud Endpoints在 Google Cloud Endpoints 中获取原始 HTTP 数据(标头、Cookie 等)
【发布时间】:2013-02-09 23:57:21
【问题描述】:

我想知道是否可以在 Cloud Endpoint 中收集原始 HTTP 数据。 我似乎在 Google 的文档中找不到任何内容,但 App Engine 的 Twitter 告诉我是 (https://twitter.com/app_engine/status/305747445017624576)。 如果是这样,我可以请它的语法吗? 我知道 GCE 的 API 仍处于早期阶段,我们将不胜感激。

【问题讨论】:

    标签: java google-app-engine google-cloud-endpoints


    【解决方案1】:

    将 HttpServletRequest 参数添加到您的端点方法,例如

    @ApiMethod
    public MyResponse getResponse( HttpServletRequest req, @Named("infoId") String infoId ) {
        // Use 'req' as you would in a servlet, e.g.
        String ipAddress = req.getRemoteAddr();
        ...
    }
    

    【讨论】:

    • 哦,哇!这非常简单。将返回结果,谢谢!
    • 是的,我偶然发现了一些问题(我不记得我在哪里找到了解决方案),但很高兴当我找到它时它是多么简单。
    • Python版本也可以吗?
    • 这绝对是正确答案!但是在 GAE 文档中哪里可以找到呢?
    【解决方案2】:

    请求在 Endpoints 方法中作为注入类型可用。当您在具有该类型的方法上声明参数时,HttpServletRequest 类型的对象会不可见地注入到您的 Java 方法定义中,如下所示:

    import javax.servlet.http.HttpServletRequest;
    ...
    
    @ApiMethod
    public MyMethod getRequest( HttpServletRequest req ) {
    
    HttpServletRequest myRequest = req;
    ...
    }
    

    这在此处记录:

    https://cloud.google.com/endpoints/docs/frameworks/java/parameter-and-return-types#injected_types

    引用上述文档:

    注入类型

    注入类型是那些受到 Cloud Endpoints Frameworks 特殊处理的类型。如果这样的类型被用作 方法参数,它不是 API 的一部分。相反, 参数由 Endpoints Frameworks 填写。

    注入的类型如下:

    com.google.appengine.api.users.User

    javax.servlet.http.HttpServletRequest

    javax.servlet.ServletContext

    【讨论】:

      猜你喜欢
      • 2017-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-27
      • 1970-01-01
      • 2016-08-20
      • 2012-06-27
      相关资源
      最近更新 更多