Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ColdFusion 10 REST API: How to set status code 201 without RestSetResponse()

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"?

like image 257
Johnny Oshika Avatar asked Mar 29 '13 20:03

Johnny Oshika


People also ask

What is the possible error response code when a post API is requested as a put API?

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.

What is Success status code?

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.

What is the http status code means created when a resource is successful created using post or put request?

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.

What is REST API code?

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.


1 Answers

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.

like image 171
Adam Cameron Avatar answered Sep 30 '22 17:09

Adam Cameron