Send HTTP 204 response using Google Cloud Endpoints

I am creating an API using Google Cloud Endpoints where I would like to return an HTTP 204 "no content" response if nothing is returned. I tried to return a null value that throws an error on the development server and a non-empty result when creating a 200 status code.

Can a true 204 be sent an empty response or other types or custom answers?

+4
source share
2 answers

To return 204 No Content for the production Python endpoint APIs, 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() 

It currently returns 200 on the development server, thanks for detecting this error!

+5
source

This probably doesn't help, but the only way I know to manipulate the status code is to throw an exception. There are a set of default exceptions that are pointed to 400, 401, 403, 404, and 500. The docs say you can subclass endpoints.ServiceException to throw other status codes, however I couldn't get this to work. If you set http_status to anything other than one of the above, it always results in 400.

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

I run the test in my handler as follows:

 raise TestException('The status should be 204') 

And I see this output when testing using an API browser:

 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" } } 
0
source

All Articles