Cheers Michael. Thanks for responding :) 

Can't seem to locate where request is favouring HTML over application/json. 
I have tested using curl request to set the accept and content-type headers 
as follows:

curl -i -H "Accept: application/json" -H "Content-Type: application/json" 
http://localhosthost:6543/assignments/101

Have also tried passing in headers dictionary with *Accept=application/json* 
and *Content-Type=application/json* values to the HTTPNotFound exception 
class but still renders HTML. The only way I can seem to get JSON response 
is if I RETURN a HTTPNotFound exception or use the notfound and 
forbidden_view decorators as suggested. Will probably go down the route of 
using the decorators to render JSON for unmatched URLs AND resource not 
found urls.

Cheers. Thanks again for your help :)

Simon

 

   

 

On Thursday, 15 December 2016 16:18:29 UTC, Michael Merickel wrote:
>
> The default exceptions raised when the framework determines notfound and 
> forbidden errors are reliant on the request's Accept headers to determine 
> which response to see. If you're seeing html it's because the request 
> favored it over application/json. You can override these exception views 
> and then return whatever response you'd like via the notfound_view_config 
> and forbidden_view_config decorators [1].
>
> As an aside, you could be doing `json_body=myErrorDict` instead of doing 
> the json.dumps yourself if you wanted.
>
> [1] 
> http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/hooks.html#changing-the-not-found-view
>
> On Thu, Dec 15, 2016 at 9:47 AM, 'dcs3spp' via pylons-discuss <
> [email protected] <javascript:>> wrote:
>
>> Hi,
>>
>> Wonder if anyone can help/advise with this query. 
>>
>> I am using the built in HTTPException hierarchy to raise a JSON exception 
>> with the use of content_type and body parameters, e.g. 
>>
>> *raise HTTPBadRequest (content_type='application/json', 
>> body=json.dumps(myErrorDict)). *
>>
>> This works fine rendering JSON for exception types other than 
>> HTTPNotFound and HTTPForbidden. 
>>
>> The documentation at 
>> http://docs.pylonsproject.org/projects/pyramid/en/latest/api/httpexceptions.html#module-pyramid.httpexceptions
>>  
>> states that when these types of exception are RAISED the appropriate 
>> default view (not found or forbidden) will be called. This is indeed the 
>> case when testing and always appear to be rendered as HTML, despite setting 
>> the content_type and body parameters as JSON. 
>>
>> I have noticed that if I RETURN HttpNotFound exception as a response then 
>> JSON is rendered, e.g. 
>> *return HTTPNotFound (content_type='application/json', 
>> body=json.dumps(myErrorDict))*
>>
>> In my application I raise a HTTPNotFound exception whenever a resource id 
>> for a GET request cannot be found in the backend database. Currently this 
>> directs me to the HTML landing view for not found errors.
>>
>> In this case is it acceptable to RETURN HTTPNotFound so that I can 
>> achieve a JSON response? Or, should I be using a different exception type 
>> when a resource cannot be located in the database?  This way I am not 
>> confusing HTTPNotFound exception with the scenario where the server did not 
>> find anything matching the Request-URI? What are other developers opinions 
>> on this?
>>
>> Cheers
>>
>> Simon
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "pylons-discuss" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected] <javascript:>.
>> To post to this group, send email to [email protected] 
>> <javascript:>.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/pylons-discuss/b3d1eb75-5e46-409d-95a5-dc10061b9009%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/pylons-discuss/b3d1eb75-5e46-409d-95a5-dc10061b9009%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pylons-discuss/0ddd8dd0-56d2-413d-92a0-577bb82da4f3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to