Re: [web2py] Re: Raising HTTP errors as JSON?
Not to worry, already upgraded all my servers On Wed, Mar 6, 2013 at 11:58 PM, Niphlod wrote: > If on old version, you can zero out the returning an iterator (or a > list) instead of the body, i.e > raise HTTP(401, [json.dumps(whatever)]) > vs > raise HTTP(401, json.dumps(whatever)) > > > On Wednesday, March 6, 2013 12:15:07 PM UTC+1, Alec Taylor wrote: >> >> 401 was actually the error code that would be relevant there. >> >> Surprised that the XML comments were removed in 2.4.2; had been >> tracing the commits. Just checked and noticed I am on 02.21.10.15.58 >> and it was fixed in 02.22.18.44.48 >.< >> >> Thanks for the other tips; should be helpful :D >> >> On Wed, Mar 6, 2013 at 9:57 PM, Ricardo Pedroso >> wrote: >> > On Wed, Mar 6, 2013 at 9:43 AM, Alec Taylor wrote: >> >> Oh, looks like it works on an endpoint level; not sure how to get rid >> >> of the XML style comment though >> >> >> >> Output from curl: >> >> >> >>> GET /arbg/error HTTP/1.1 >> >>> Host: localhost >> >>> Accept: */* >> >>> >> >> >> >> < HTTP/1.1 404 NOT FOUND >> >> < Set-Cookie: >> >> session_id_arbg=127.0.0.1-d5613ca1-ff65-4c0e-a5ce-737c6024655d; >> >> Path=/ >> >> < Content-Length: 555 >> >> < Content-Type: text/html; charset=UTF-8 >> >> < Date: Wed, 06 Mar 2013 09:39:20 GMT >> >> < Server: Rocket 1.2.6 Python/2.7.3 >> >> < Connection: keep-alive >> >> < >> >> {"error": "unauthorized_client"} >> > >> > I started using this: >> > >> > raise HTTP(status, body, None, **{'Content-Type': 'application/json'}) >> > >> > where >> > status - an HTTP status code (200, 404, 500, ...) >> > body - a json.dumps("something") >> > >> > This way I don't need to put a .json extension in the end of url >> > >> > To get rid of those xxx's you can upgrade to 2.4.2 >> > >> > Note: web2py return a "Content-Type: text/plain; charset=utf-8" when >> > using a .json extension >> > I think it for max compatibility. >> > >> > PS: In your example your are issuing a 404 where a 403 would be a better >> > fit >> > >> > Ricardo >> > >> > -- >> > >> > --- >> > You received this message because you are subscribed to the Google >> > Groups "web2py-users" group. >> > To unsubscribe from this group and stop receiving emails from it, send >> > an email to web2py+un...@googlegroups.com. >> > For more options, visit https://groups.google.com/groups/opt_out. >> > >> > > > -- > > --- > You received this message because you are subscribed to the Google Groups > "web2py-users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to web2py+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > > -- --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: [web2py] Re: Raising HTTP errors as JSON?
If on old version, you can zero out the returning an iterator (or a list) instead of the body, i.e raise HTTP(401, [json.dumps(whatever)]) vs raise HTTP(401, json.dumps(whatever)) On Wednesday, March 6, 2013 12:15:07 PM UTC+1, Alec Taylor wrote: > > 401 was actually the error code that would be relevant there. > > Surprised that the XML comments were removed in 2.4.2; had been > tracing the commits. Just checked and noticed I am on 02.21.10.15.58 > and it was fixed in 02.22.18.44.48 >.< > > Thanks for the other tips; should be helpful :D > > On Wed, Mar 6, 2013 at 9:57 PM, Ricardo Pedroso > > > wrote: > > On Wed, Mar 6, 2013 at 9:43 AM, Alec Taylor > > > > wrote: > >> Oh, looks like it works on an endpoint level; not sure how to get rid > >> of the XML style comment though > >> > >> Output from curl: > >> > >>> GET /arbg/error HTTP/1.1 > >>> Host: localhost > >>> Accept: */* > >>> > >> > >> < HTTP/1.1 404 NOT FOUND > >> < Set-Cookie: > session_id_arbg=127.0.0.1-d5613ca1-ff65-4c0e-a5ce-737c6024655d; > >> Path=/ > >> < Content-Length: 555 > >> < Content-Type: text/html; charset=UTF-8 > >> < Date: Wed, 06 Mar 2013 09:39:20 GMT > >> < Server: Rocket 1.2.6 Python/2.7.3 > >> < Connection: keep-alive > >> < > >> {"error": "unauthorized_client"} > > > > I started using this: > > > > raise HTTP(status, body, None, **{'Content-Type': 'application/json'}) > > > > where > > status - an HTTP status code (200, 404, 500, ...) > > body - a json.dumps("something") > > > > This way I don't need to put a .json extension in the end of url > > > > To get rid of those xxx's you can upgrade to 2.4.2 > > > > Note: web2py return a "Content-Type: text/plain; charset=utf-8" when > > using a .json extension > > I think it for max compatibility. > > > > PS: In your example your are issuing a 404 where a 403 would be a better > fit > > > > Ricardo > > > > -- > > > > --- > > You received this message because you are subscribed to the Google > Groups "web2py-users" group. > > To unsubscribe from this group and stop receiving emails from it, send > an email to web2py+un...@googlegroups.com . > > For more options, visit https://groups.google.com/groups/opt_out. > > > > > -- --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: [web2py] Re: Raising HTTP errors as JSON?
FYI: I get: Content-Type: application/json When I have: `response.view = 'generic.json'`, e.g.: on a `@request.restful()` On just a regular GET request on a `@service.json` endpoint I get: Content-Type: text/html; charset=UTF-8 And with a POST that has its args given with `-d` curl arg I get: Content-Type: application/x-www-form-urlencoded With reply (same reply whether containing .json in URL or not): Content-Type: text/html; charset=UTF-8 On Wed, Mar 6, 2013 at 10:15 PM, Alec Taylor wrote: > 401 was actually the error code that would be relevant there. > > Surprised that the XML comments were removed in 2.4.2; had been > tracing the commits. Just checked and noticed I am on 02.21.10.15.58 > and it was fixed in 02.22.18.44.48 >.< > > Thanks for the other tips; should be helpful :D > > On Wed, Mar 6, 2013 at 9:57 PM, Ricardo Pedroso wrote: >> On Wed, Mar 6, 2013 at 9:43 AM, Alec Taylor wrote: >>> Oh, looks like it works on an endpoint level; not sure how to get rid >>> of the XML style comment though >>> >>> Output from curl: >>> GET /arbg/error HTTP/1.1 Host: localhost Accept: */* >>> >>> < HTTP/1.1 404 NOT FOUND >>> < Set-Cookie: >>> session_id_arbg=127.0.0.1-d5613ca1-ff65-4c0e-a5ce-737c6024655d; >>> Path=/ >>> < Content-Length: 555 >>> < Content-Type: text/html; charset=UTF-8 >>> < Date: Wed, 06 Mar 2013 09:39:20 GMT >>> < Server: Rocket 1.2.6 Python/2.7.3 >>> < Connection: keep-alive >>> < >>> {"error": "unauthorized_client"} >> >> I started using this: >> >> raise HTTP(status, body, None, **{'Content-Type': 'application/json'}) >> >> where >> status - an HTTP status code (200, 404, 500, ...) >> body - a json.dumps("something") >> >> This way I don't need to put a .json extension in the end of url >> >> To get rid of those xxx's you can upgrade to 2.4.2 >> >> Note: web2py return a "Content-Type: text/plain; charset=utf-8" when >> using a .json extension >> I think it for max compatibility. >> >> PS: In your example your are issuing a 404 where a 403 would be a better fit >> >> Ricardo >> >> -- >> >> --- >> You received this message because you are subscribed to the Google Groups >> "web2py-users" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to web2py+unsubscr...@googlegroups.com. >> For more options, visit https://groups.google.com/groups/opt_out. >> >> -- --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: [web2py] Re: Raising HTTP errors as JSON?
401 was actually the error code that would be relevant there. Surprised that the XML comments were removed in 2.4.2; had been tracing the commits. Just checked and noticed I am on 02.21.10.15.58 and it was fixed in 02.22.18.44.48 >.< Thanks for the other tips; should be helpful :D On Wed, Mar 6, 2013 at 9:57 PM, Ricardo Pedroso wrote: > On Wed, Mar 6, 2013 at 9:43 AM, Alec Taylor wrote: >> Oh, looks like it works on an endpoint level; not sure how to get rid >> of the XML style comment though >> >> Output from curl: >> >>> GET /arbg/error HTTP/1.1 >>> Host: localhost >>> Accept: */* >>> >> >> < HTTP/1.1 404 NOT FOUND >> < Set-Cookie: >> session_id_arbg=127.0.0.1-d5613ca1-ff65-4c0e-a5ce-737c6024655d; >> Path=/ >> < Content-Length: 555 >> < Content-Type: text/html; charset=UTF-8 >> < Date: Wed, 06 Mar 2013 09:39:20 GMT >> < Server: Rocket 1.2.6 Python/2.7.3 >> < Connection: keep-alive >> < >> {"error": "unauthorized_client"} > > I started using this: > > raise HTTP(status, body, None, **{'Content-Type': 'application/json'}) > > where > status - an HTTP status code (200, 404, 500, ...) > body - a json.dumps("something") > > This way I don't need to put a .json extension in the end of url > > To get rid of those xxx's you can upgrade to 2.4.2 > > Note: web2py return a "Content-Type: text/plain; charset=utf-8" when > using a .json extension > I think it for max compatibility. > > PS: In your example your are issuing a 404 where a 403 would be a better fit > > Ricardo > > -- > > --- > You received this message because you are subscribed to the Google Groups > "web2py-users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to web2py+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > > -- --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: [web2py] Re: Raising HTTP errors as JSON?
On Wed, Mar 6, 2013 at 9:43 AM, Alec Taylor wrote: > Oh, looks like it works on an endpoint level; not sure how to get rid > of the XML style comment though > > Output from curl: > >> GET /arbg/error HTTP/1.1 >> Host: localhost >> Accept: */* >> > > < HTTP/1.1 404 NOT FOUND > < Set-Cookie: session_id_arbg=127.0.0.1-d5613ca1-ff65-4c0e-a5ce-737c6024655d; > Path=/ > < Content-Length: 555 > < Content-Type: text/html; charset=UTF-8 > < Date: Wed, 06 Mar 2013 09:39:20 GMT > < Server: Rocket 1.2.6 Python/2.7.3 > < Connection: keep-alive > < > {"error": "unauthorized_client"} I started using this: raise HTTP(status, body, None, **{'Content-Type': 'application/json'}) where status - an HTTP status code (200, 404, 500, ...) body - a json.dumps("something") This way I don't need to put a .json extension in the end of url To get rid of those xxx's you can upgrade to 2.4.2 Note: web2py return a "Content-Type: text/plain; charset=utf-8" when using a .json extension I think it for max compatibility. PS: In your example your are issuing a 404 where a 403 would be a better fit Ricardo -- --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: [web2py] Re: Raising HTTP errors as JSON?
Oh, looks like it works on an endpoint level; not sure how to get rid of the XML style comment though Output from curl: > GET /arbg/error HTTP/1.1 > Host: localhost > Accept: */* > < HTTP/1.1 404 NOT FOUND < Set-Cookie: session_id_arbg=127.0.0.1-d5613ca1-ff65-4c0e-a5ce-737c6024655d; Path=/ < Content-Length: 555 < Content-Type: text/html; charset=UTF-8 < Date: Wed, 06 Mar 2013 09:39:20 GMT < Server: Rocket 1.2.6 Python/2.7.3 < Connection: keep-alive < {"error": "unauthorized_client"} FYI: I am running web2py version 2.4.1-alpha.2+timestamp.2013.02.21.10.15.58 On Wed, Mar 6, 2013 at 8:15 PM, Niphlod wrote: > what did you expect ? > > On Wednesday, March 6, 2013 9:37:26 AM UTC+1, Alec Taylor wrote: >> >> How do I raise HTTP errors as JSON? >> >> My attempt: >> >> In [1]: import json >> >> In [2]: from gluon.http import HTTP >> >> In [3]: raise HTTP(412, json.dumps({'error': 'must accept terms'})) >> >> --- >> HTTP Traceback (most recent call >> last) >> in () >> > 1 raise HTTP(412, json.dumps({'error': 'must accept terms'})) >> >> HTTP: 412 PRECONDITION FAILED > > -- > > --- > You received this message because you are subscribed to the Google Groups > "web2py-users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to web2py+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > > -- --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.