24

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.

bodega
  • 1,005
  • 1
  • 9
  • 7

2 Answers2

48

Add an HttpServletRequest parameter to your endpoint method, e.g.

@ApiMethod
public MyResponse getResponse( HttpServletRequest req, @Named("infoId") String infoId ) {
    // Use 'req' as you would in a servlet, e.g.
    String ipAddress = req.getRemoteAddr();
    ...
}
Tom
  • 17,103
  • 8
  • 67
  • 75
  • Oh, wow! That is surprisingly simple. Will post back with results, thank you! – bodega Feb 24 '13 at 22:24
  • Yeah, I stumbled around a bit trying to figure it out (I can't remember where I found the solution) but was pleased with how simple it was when I did find it. – Tom Feb 24 '13 at 22:46
  • 1
    Probably from here: https://developers.google.com/appengine/docs/java/endpoints/paramreturn_types#injected_types – Ivan Nov 03 '13 at 18:44
  • 7
    Is this also possible with the Python version? – Korneel Dec 18 '13 at 08:39
  • 1
    This is absolutely the right answer! But where can this be found in GAE docu? – Yoraco Gonzales May 25 '15 at 10:27
  • How would I do this for HttpServletResponse? (I want to serve an mp4 video through endpoint) so I need the HttpServletResponse to pass to the Blobstore api – Katedral Pillon Oct 13 '15 at 22:25
0

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

Community
  • 1
  • 1
Chris Halcrow
  • 28,994
  • 18
  • 176
  • 206