Send HTTP 204 response with Google Cloud Endpoints

36 views
Skip to first unread message

botteaap via StackOverflow

unread,
Mar 3, 2013, 3:35:42 PM3/3/13
to google-appengin...@googlegroups.com

I'm creating an API using Google Cloud Endpoints where I would like to return a "no content" HTTP 204 response if there's nothing to return. I tried returning null, which throws an error on the development server, and a non-empty result on production with status code 200.

It is possible to send out a true 204 empty response or other types or custom responses?



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/15190471/send-http-204-response-with-google-cloud-endpoints

antsyawn via StackOverflow

unread,
Mar 5, 2013, 7:47:47 AM3/5/13
to google-appengin...@googlegroups.com

This probably doesn't help, but the only way I know to manipulate the status code is by raising an exception. There are a default set of exceptions provided which map to 400, 401, 403, 404 and 500. The docs say you can subclass endpoints.ServiceException to generate other status codes, however I haven't been able to get this to work. If you set http_status to anything other than one of those listed above, it always results in a 400.



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/15190471/send-http-204-response-with-google-cloud-endpoints/15224181#15224181

antsyawn via StackOverflow

unread,
Mar 6, 2013, 1:50:08 AM3/6/13
to google-appengin...@googlegroups.com

This probably doesn't help, but the only way I know to manipulate the status code is by raising an exception. There are a default set of exceptions provided which map to 400, 401, 403, 404 and 500. The docs say you can subclass endpoints.ServiceException to generate other status codes, however I haven't been able to get this to work. If you set http_status to anything other than one of those listed above, it always results in a 400.

class TestException(endpoints.ServiceException):
    http_status = httplib.NO_CONTENT

I run a test in my handler like this:

raise TestException('The status should be 204')

And I see this output when testing it using the API explorer:

400 Bad Request

- Show headers -

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "badRequest",
    "message": "The status should be 204"
   }
  ],
  "code": 400,
  "message": "The status should be 204"
 }
}

bossylobster via StackOverflow

unread,
Mar 6, 2013, 11:35:51 AM3/6/13
to google-appengin...@googlegroups.com

To return a 204 No Content for a production Python Cloud Endpoints API, you can use VoidMessage.

from google.appengine.ext import endpoints
from protorpc import messages
from protorpc import message_types
from protorpc import remote

class MyMessage(messages.Message):
  ...

@endpoints.api('someapi', 'v1', 'Description')
class MyApi(remote.Service):

  @endpoints.method(MyMessage, message_types.VoidMessage,
                    ...)
  def my_method(self, request):
    ...
    return message_types.VoidMessage()

This currently returns a 200 on the development server, thanks for finding this bug!



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/15190471/send-http-204-response-with-google-cloud-endpoints/15252925#15252925
Reply all
Reply to author
Forward
0 new messages