I'm using ColdFusion 10's new built-in REST API and I'd like to return a status code of 201 (Created). I first tried the RestSetResponse() approach that's described here: http://www.adobe.com/devnet/coldfusion/articles/restful-web-services.html. It works well, except that it forces you to set the function's returntype to "void". The problem with "void" is that whenever I throw an exception, it no longer returns the proper JSON error message.
Throwing this exception:
<cfthrow errorcode="400" message="Validation error." />
Returns a nicely formatted JSON when the returntype is "struct":
HTTP/1.1 400 Bad Request
Content-Type: application/json
{"Message":"Validation error."}
But when the returntype is "void" (which is required to use RestSetResponse(), the response is some ugly HTML response.
Because of this, I had to revert to using returntype "struct", gave up on RestSetResponse(), and tried this:
<cfheader statusCode="201" statusText="Created" />
But it doesn't work. It seems that ColdFusion overwrites the statusCode and always returns 200 (OK) when it's successful. Anyone know of a way to change the status code to 201 without setting the returntype of the function to "void"?
The 204 status code is usually sent out in response to a PUT , POST , or DELETE request when the REST API declines to send back any status message or representation in the response message's body.
CREATED 201 Following a POST command, this indicates success, but the textual part of the response line indicates the URI by which the newly created document should be known.
D - 304. Q 5 - Which of the following HTTP Status code means CREATED, when a resource is successful created using POST or PUT request? A - 200.
A RESTful API is an architectural style for an application program interface (API) that uses HTTP requests to access and use data. That data can be used to GET, PUT, POST and DELETE data types, which refers to the reading, updating, creating and deleting of operations concerning resources.
I can't see a good reason why restSetResponse()
should require a returntype of void, but have verified it is ignored if this is not the case. Which is a bit rubbish.
The only thing I can think by way of working around your situation is to roll-your-own struct with the error detail in it, then use that as the content
value set for the restSetResponse()
call.
This is a bit jerry-built, but you're constrainted by the jerry-built-ness of ColdFusion in this instance, I think.
I've logged a bug relating to this.
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