Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set response header in Google App Engine Endpoints (Java)

I'm implementing a RESTful service using GAE Endpoints in Java. I'm trying to implement token-based authentication method and I wish to add a HTTP response header that contains the token string. However, I'm unable to find documentations on how to change the response header inside a @ApiMethod.

Anybody can shed some light on this? Any help very much appreciated.

like image 958
hsiaomijiou Avatar asked Oct 20 '25 03:10

hsiaomijiou


2 Answers

From the looks of things, it isn't possible. There is another Stack Overflow Q&A on this question which answered the same way. You can make a Feature Request at the App Engine Public Issue Tracker, of course.

Note: Attempts to use javax.servlet.Filter to intercept the ServletResponse, cast it to an HttpServletResponse and call .setHeader() on it yielded no success, so it appears there are other layers which are intercepting the response and wrapping up one without any headers added which aren't "meant" to be there, according to Endpoints. I didn't experiment with subclassing SystemServiceServlet, as the linked Q&A suggested, although that seems fraught with difficulties as well, and undocumented.

like image 187
Nick Avatar answered Oct 23 '25 10:10

Nick


I've checked there is no way to change response header. More over @Nick has right that there is more layers - on the servlet container there is code which wraps response.

Some layers above - evidence is that error messages are packaged for endpoints. SystemServletService is putting into body this:

{
  "error_message": "Some message"
}

Bu in practice response you get contains:

{
    "error": {
        "message": "Some messaged",
        "code": 401,
        "errors": [
            {
                "domain": "global",
                "reason": "required",
                "message": "Some message"
            }
        ]
    }
}

For endpoints (SystemServiceServlet) only, for my custom servlets it's not. This is evidence that there is some layer above. ;)

like image 27
sratatata Avatar answered Oct 23 '25 10:10

sratatata



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!