简体   繁体   English

在Google Cloud Endpoints中获取原始HTTP数据(标题,Cookie等)

[英]Getting raw HTTP Data (Headers, Cookies, etc) in Google Cloud Endpoints

I am wondering if it is possible to collect raw HTTP data in a Cloud Endpoint. 我想知道是否有可能在Cloud Endpoint中收集原始HTTP数据。 I can't seem to find anything in Google's documentation, but App Engine's Twitter told me that it was ( https://twitter.com/app_engine/status/305747445017624576 ). 我似乎无法在Google的文档中找到任何内容,但App Engine的Twitter告诉我它是( https://twitter.com/app_engine/status/305747445017624576 )。 If so, can I please have syntax for it? 如果是这样,我可以请它的语法吗? I am aware that the API for GCE is still in its early stages, and any help would be greatly appreciated. 我知道GCE的API仍处于早期阶段,任何帮助都将不胜感激。

Add an HttpServletRequest parameter to your endpoint method, eg 将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();
    ...
}

The request is available in an Endpoints method as an injected type . 该请求在Endpoints方法中可用作注入类型 An object of type HttpServletRequest is invisibly injected into your Java method definition when you declare a parameter on the method that has that type, like this: 当您在具有该类型的方法上声明参数时,将无形中将HttpServletRequest类型的对象注入到Java方法定义中,如下所示:

import javax.servlet.http.HttpServletRequest;
...

@ApiMethod
public MyMethod getRequest( HttpServletRequest req ) {

HttpServletRequest myRequest = req;
...
}

This is documented here: 这在此处记录:

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

Quoting from the above documentation: 引自上述文件:

Injected types 注入类型

Injected types are those types that receive special treatment by Cloud Endpoints Frameworks. 注入类型是由Cloud Endpoints Frameworks接受特殊处理的类型。 If such a type is used as a method parameter, it isn't made a part of the API. 如果将此类型用作方法参数,则不会将其作为API的一部分。 Instead, the parameter is filled in by Endpoints Frameworks. 相反,该参数由Endpoints Frameworks填充。

The injected types are the following: 注入的类型如下:

com.google.appengine.api.users.User com.google.appengine.api.users.User

javax.servlet.http.HttpServletRequest javax.servlet.http.HttpServletRequest

javax.servlet.ServletContext javax.servlet.ServletContext

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM