Returning custom HTTP status codes from endpoints

304 views
Skip to first unread message

Matthew Flint

unread,
May 27, 2014, 4:06:44 PM5/27/14
to google-a...@googlegroups.com
Hello team,

We all love REST-full-ness, so I'd like to return more REST-ful status codes from my endpoints. (Example: 201 CREATED from a POST endpoint which creates a resource).

But I can't find any way to set the HTTP status code. Can anyone help please?

Some other (sad-path) status codes seem to be driven by throwing an exception - eg. NotFoundException causes a 404. But I want to return a different status code *and* XML/JSON in the response.

Thanks all!
  Matthew

Kristopher Giesing

unread,
Jun 1, 2014, 12:42:25 PM6/1/14
to google-a...@googlegroups.com
You don't mention which language you're using. If this is Java, you'd call response.sendError(int errorCode, String message) on the servlet's HttpServletResponse input argument, but I don't know offhand what it would be in Go, Python, or PHP.

- Kris

alex

unread,
Jun 1, 2014, 3:16:37 PM6/1/14
to google-a...@googlegroups.com
I don't believe this is currently supported. There's a similar issue created specifically for Java runtime:

You could create a new one w/o specifying a runtime as a generic feature request.

Cheers.

Vinny P

unread,
Jun 2, 2014, 12:43:55 AM6/2/14
to google-a...@googlegroups.com
Endpoints filter out certain status codes, so you can't do (as in your example) 201 Created. Here's a list of the status codes you can return: https://developers.google.com/appengine/docs/python/endpoints/exceptions 

Although if you weren't using Endpoints, you could manually set the status code with resp.setStatus(int)http.Error(w, "error", code), etc.
 
 
-----------------
-Vinny P
Technology & Media Advisor
Chicago, IL

App Engine Code Samples: http://www.learntogoogleit.com

Kristopher Giesing

unread,
Jun 3, 2014, 2:38:35 AM6/3/14
to google-a...@googlegroups.com
Sorry, I glossed over that you're using endpoints, not writing servlet code directly.  My answer probably doesn't help.

Dave Goldberg

unread,
Jun 7, 2014, 12:59:58 PM6/7/14
to google-a...@googlegroups.com
I return custom wrapper object with a field for my own internal status codes. The wrapper object is a generic class so the playload/data can be whatever I need to return.

Essentially unless something goes wrong my Endpoints always return 200. The caller has to inspect the object returned to get the true status code. It works fine but I loose a bit of type safety from my endpoints as it's not always clear what type of data the endpoint returns. 
Reply all
Reply to author
Forward
0 new messages