Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning RESTful response codes in Play

I'm just getting started with REST and I've been reading this post and the mentioned book about REST response codes. When I look at Play's Controller class however, it seems to be limited to returning

  • 200 - OK
  • 301 - Moved Permanently
  • 302 - Found
  • 304 - Not Modified
  • 400 - Bad Request
  • 401 - Not Authorized
  • 403 - Forbidden
  • 404 - Not Found
  • 5xx

That seems to leave out some potentially useful codes that were mentioned:

  • 201 - Created (good response for successful JSON post?)
  • 202 - Accepted (for queued requests)
  • 204 - No Content (possible response for successful PUT/POST/DELETE)
  • 307 - Temporary Redirect
  • 405 - Method Not Allowed
  • 406 - Not Acceptable
  • 409 - Conflict
  • 410 - Gone
  • 415 - Unsupported Media Type (this seems like the appropriate response for a request for JSON format when no JSON template is defined)

Are those not needed after all? Is Play handling those situations automatically?

Also it appears that one controller can't handle REST requests and normal web page requests for the same resource very well since the web pages are always returned with 200. Am I missing anything there?

like image 313
Brad Mace Avatar asked Dec 17 '10 18:12

Brad Mace


2 Answers

Looking at the Play Source code (Play 1.1) at the play.mvc.Http.StatusCode object, Play appears to have the following codes

public static final int OK = 200;
public static final int CREATED = 201;
public static final int ACCEPTED = 202;
public static final int PARTIAL_INFO = 203;
public static final int NO_RESPONSE = 204;
public static final int MOVED = 301;
public static final int FOUND = 302;
public static final int METHOD = 303;
public static final int NOT_MODIFIED = 304;
public static final int BAD_REQUEST = 400;
public static final int UNAUTHORIZED = 401;
public static final int PAYMENT_REQUIERED = 402;
public static final int FORBIDDEN = 403;
public static final int NOT_FOUND = 404;
public static final int INTERNAL_ERROR = 500;
public static final int NOT_IMPLEMENTED = 501;
public static final int OVERLOADED = 502;
public static final int GATEWAY_TIMEOUT = 503;

This would indicate acknowledgement of SOME of the codes your have identified, such as 201, 202, 204. However, the values 307, 405, 406, 409, 410 and 415 are not there.

Also, 201, 202, 204 are acknowledged, but are not referenced anywhere else within the source code. So unless the Netty server, or one of the supplied jar files is managing these for Play (which I am not sure it can do), I can't see how Play can magically handle these situations without knowing the code base.

Looking at the code for renderJSON, it does not appear to set the status code as part of sending the results back (so uses the default 200), so the following hack may work.

public static void myJsonAction() {
    response.status = 201;
    renderJSON(jsonString); // replace with your JSON String
}
like image 92
Codemwnci Avatar answered Nov 09 '22 07:11

Codemwnci


In the current Play version you have to use status() instead. Example:

status(201, jsonData);

In Scala it should work as in this example taken from the official docs:

Status(488)("Strange response type")
like image 45
lex82 Avatar answered Nov 09 '22 06:11

lex82