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
That seems to leave out some potentially useful codes that were mentioned:
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?
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
}
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With