简体   繁体   中英

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

Add an HttpServletRequest parameter to your endpoint method, eg

@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 . 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:

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

Quoting from the above documentation:

Injected types

Injected types are those types that receive special treatment by Cloud Endpoints Frameworks. If such a type is used as a method parameter, it isn't made a part of the API. Instead, the parameter is filled in by Endpoints Frameworks.

The injected types are the following:

com.google.appengine.api.users.User

javax.servlet.http.HttpServletRequest

javax.servlet.ServletContext

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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