New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom response http codes in renderWith() method for web API application #549

Closed
larshorner opened this Issue Aug 26, 2015 · 9 comments

Comments

6 participants
@larshorner

larshorner commented Aug 26, 2015

I am currently working on a project that uses CFWheels for the application layer and I was asked to create a web API to allow for 3rd party integration. We have the requirement to send custom response codes to indicate success and failure on api calls (i.e. 200 success, 403 not authorized, 500 error occurred) and currently the renderWith method does not support such a thing.

My suggestion is to add a new argument of type numeric, not required and default 200 to the current renderWith() method. This argument can be checked for existence and used to set response header as required.

Please give me your thoughts.

@tapirus

This comment has been minimized.

Contributor

tapirus commented Aug 26, 2015

Maybe habe a look at taffy how it's solved there.

@larshorner

This comment has been minimized.

larshorner commented Aug 26, 2015

This is what I have done in my code base for now.

I modified the /wheels/controller/provides.cfm
add this to line 55
<cfargument name="responseCode" type="numeric" required="false" default="200">

and this to line ~99

if(structKeyExists(arguments, 'responseCode')){
    getPageContext().getResponse().setStatus( arguments.responseCode );
}

now I call it like this: renderWith(data = {some:'data'}, responseCode = 403);

Works great for me, If you like my idea I will put it in a pull request.

Lars

@perdjurner

This comment has been minimized.

Contributor

perdjurner commented Feb 2, 2016

Does it make sense to make this more generic and add to renderWith, renderPage, renderText etc?

@andybellenie

This comment has been minimized.

Contributor

andybellenie commented Feb 2, 2016

I like that idea.

How about handling status text also?

Sent from my iPhone

On 2 Feb 2016, at 10:07, Per Djurner notifications@github.com wrote:

Does it make sense to make this more generic and add to renderWith, renderPage, renderText etc?


Reply to this email directly or view it on GitHub.

@chrisdpeters

This comment has been minimized.

Contributor

chrisdpeters commented Feb 2, 2016

You may find these resources from Ruby useful:

I find the Rails one with symbols to be rather useful and more readable within the code. But perhaps some people would rather enter the numeric code.

Possible example of how it could work in CFWheels:

<!--- This... --->
<cfset renderWith(data=widget.allErrors(), status="unprocessableEntity")>

<!--- and/or this... --->
<cfset renderWith(data=widget.allErrors(), status=422)>
@andybellenie

This comment has been minimized.

Contributor

andybellenie commented Feb 2, 2016

:)

@tapirus

This comment has been minimized.

Contributor

tapirus commented Feb 2, 2016

Nice! I like the idea to have the choice to either enter text or the code.

Andrew Bellenie <notifications@github.com> schrieb am 16:17 Dienstag, 2.Februar 2016:

:)—
Reply to this email directly or view it on GitHub.

@neokoenig neokoenig self-assigned this Apr 1, 2016

@neokoenig neokoenig closed this in 21dcb14 Apr 1, 2016

@neokoenig neokoenig added this to the 2.0.0 milestone Apr 1, 2016

@neokoenig

This comment has been minimized.

Member

neokoenig commented Apr 1, 2016

ok, so renderWith() can now accept a status argument;

Either by numerical value, i,e:
renderWith(data=data, status=200);

or via text value:
renderWith(data={}, status="No Content");

...which sets the cfheader values appropriately.

The latter is essentially a reverse lookup of the former, so only really useful if you know the appropriate http status text. I imagine most people will use "ok", or "Not Found", or "No Content" etc; If you pass in 00007 or THECAKEISALIE is will throw an appropriate Wheels error

@neokoenig neokoenig reopened this Apr 1, 2016

@neokoenig neokoenig added the docs label Apr 1, 2016

@neokoenig

This comment has been minimized.

Member

neokoenig commented Jul 5, 2016

@neokoenig neokoenig closed this Jul 5, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment