On 23.02.2010, at 10:24, [email protected] wrote:
Questions to the list: 1. Would you recommend always remapping POST to "create" when building REST APIs with Agavi?
Yes, I think that makes sense. You could always use the routing for that when adding REST APIs to existing apps:
<route pattern="…" constraint="write" method="create" /> <route pattern="…" constraint="create" method="write" />
2. Is there any suggestion to improve the routing configuration for the /books route? 2a. Currently the REST endpoint is /books.xml or /books.json depending on the format required. How could I alter this so that the router looks for a ?format=xml|json parameter and sets the output type accordingly?
<route pattern="^xml$" source="_GET[format]" output_type="xml" /> (that needs adding of $_GET as a routing source, see AgaviRouting.class.php in initialize() or startup()).
But that is not a good approach. You should instead use HTTP Accept and Content-Type headers:
<route pattern="^application/json" source="_SERVER[HTTP_ACCEPT]" stop="false" output_type="json" />
3. For DELETE operations using the XML API, is it correct to return a 204 response code and no content at all? Or should I return a 200 code and an XML document with a <status>OK</status> type of body?
You return a 200 if you also send back content with more info on the status. Alternatively, you can send a 204 and no content, just as you described. Your choice.
Note that you also have the option of sending 202 if you haven't deleted it yet (e.g. when an administrator needs to manually approve the operation).
4. I'm setting an attribute in each execute() method indicating the HTTP request method. I then check this in the view to decide whether to send a 201 or 204 status code to the client. I'm pretty sure there's a better way to do this...
executeRead(), executeWrite() and so forth!? And then separate views...
5. In case of errors in a POST/PUT/DELETE operation, what response should I send back for (a) JSON and (b) XML requests?
You should send back whatever content type the client indicated (in Accept:), or, if that wasn't done, whatever matches his Content-Type header. As content, you may send back a response content with more information.
Status codes I guess should be 400, 404, 405, 406, 409, 500 or 501 depending on the situation.
- David
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________ users mailing list [email protected] http://lists.agavi.org/mailman/listinfo/users
