how to respond with a 403

18 views
Skip to first unread message

Dave

unread,
Feb 17, 2012, 2:59:04 PM2/17/12
to sinatrarb
Hi,

I am little stumped about how to do the following:

- I authorize most requests and if the session is good, I return JSON
to certain routes.
- if the session is terminated... and a JSON request comes in... I
want to trap that..

I do the following...

content_type :json
unless authorized!
status 403
{:message => "Sorry, session is terminated"}.to_json
else
.... good to go

Problem is, my Ajax handler on the client is not being hit with a nice
JSON response and a status of 403... Is there a better way to return
JSON with the status set other than making out like all is well with a
200 and parsing out a made up status of say 403???

Dave

unread,
Feb 17, 2012, 3:15:48 PM2/17/12
to sinatrarb
I figured it out.. just had to trap the error properly on the client
side...

Michael Gorsuch

unread,
Feb 17, 2012, 3:08:12 PM2/17/12
to sina...@googlegroups.com
Hi Dave,

How about this:

halt 403, {'Content-Type' => 'application/json'}, { message: 'Sorry,
session is terminated' }.to_json

Best,

Michael

> --
> You received this message because you are subscribed to the Google Groups "sinatrarb" group.
> To post to this group, send email to sina...@googlegroups.com.
> To unsubscribe from this group, send email to sinatrarb+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sinatrarb?hl=en.
>

Reply all
Reply to author
Forward
0 new messages