Rectangle 27 0

python Flask error handling: "Response object is not iterable"?


@app.errorhandler(404)
def not_found(error):
    resp = jsonify({'error':'not found'})
    resp.status_code = 404
    return resp

As mentioned in the comments for the previous answer, that code isn't supported on Flask 0.8, and would require 0.9 or higher. If you need to support Flask 0.8, here is a compatible version that assigns the "status_code" instead:

This is wonderful! Thanks for taking the time to provide a compatible snippet for those of us lagging behind with an older version!!

Note
Rectangle 27 0

python Flask error handling: "Response object is not iterable"?


curl http://localhost:5000/
flask.jsonify
from flask import Flask, jsonify

app = Flask(__name__)

@app.errorhandler(404)
def not_found(error):
    return jsonify({'error':'not found'}), 404

app.run()
{
  "error": "not found"
}

Turns out I was on .8 -__- problem solved

Which version of flask do you use? I used 0.10.1.

Yeah I believe so. I updated my code include the rest of my code with the imports. I even copy pasted directly from your post and it gives me the same not iterable response

Note