Your frontend that's making the ajax call is likely not configuring the accept header appropriately to prefer application/json content.
On Thu, Jan 10, 2019 at 10:57 PM Mike Orr <[email protected]> wrote: > Is there a simple way to convert a Response to an HTTP string; i.e., > with headers and a body like it would send to the client? > > I have a JSON backend view that sends an HTTP exception like this: > > # HTTP 422, simulating a validation error > jreq = {"email": "[email protected]", "errors": {"email": "Test error."}} > raise pyramid.httpexceptions.HTTPUnprocessableEntity(json=jreq) > > The frontend makes an AJAX call using Backbone Model.save(), and in > the error callback the response is status 422 as it should be, but it > doesn't appear to contain the data. .responseJSON contains the kind of > things that appear on an exception page, and my data object doesn't > appear to be anywhere. > > So I wrote a little Python program to see what the exception object > contains, and its '_app_iter' attribute contains the expected JSON. > Then I wanted to see what kind of HTTP document it would produce, and > I realized I don't know how to do that on the console without a WSGI > server. > > I looked in the httpexceptions and response docs and didn't see any > related method. I then thought maybe it's just 'str()'. But 'str()' > returns "Unable to process the contained instructions". Curious. I > then looked at the 'wsgiref' docs but didn't see anything useful. Is > there a function or method that does this, or only a WSGI server has > the code? > > Second, what is the solution to my underlying problem? I want to echo > back the input items and add an "errors" key containing the errors for > the invalid fields. I read that HTTP 422 Unprocessable Entity is the > correct response to a request that contains syntactically valid JSON > but the data values are invalid. > > -- > Mike Orr <[email protected]> > > -- > 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/CAH9f%3Dup9q%3D5tKHw-pv5Fgx6pXm77R08tROxPd%2BLrfKrtuiBEMA%40mail.gmail.com > . > 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/CAKdhhwE3RJHf45HUrz597xEtc97CKbJ2cWvcZFxE2TDZiy__dg%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
