Reputation: 83
I'm following a tutorial on how to design a REST API using Flask in Python. It's located here:
https://blog.miguelgrinberg.com/post/designing-a-restful-api-with-python-and-flask
Everything went smoothly up until the POST segment. I followed everything precisely, however I keep getting a TypeError "unhashable type: 'dict'" from "return jsonify{'task', task})". I've looked over the sample code and my own dozens of times and see no difference. Here's what I have:
@app.route('/todo/api/v1.0/tasks', methods=['POST'])
def create_task():
if not request.json or not 'title' in request.json:
abort(400)
task = {
'id': tasks[-1]['id'] + 1,
'title': request.json['title'],
'description': request.json.get('description', ""),
'done': False
}
tasks.append(task)
return jsonify({'task', task}), 201
Given that this tutorial is 4 years old, has something changed in that time that makes the example invalid for some reason. I'm running it in Python 2.7.3.
For further context, here is my full script:
from flask import Flask, jsonify, make_response, abort, request
app = Flask(__name__)
tasks = [
{
'id': 1,
'title': u'Buy groceries',
'description': u'Milk, Cheese, Pizza, Fruit, Tylenol',
'done': False
},
{
'id': 2,
'title': u'Learn Python',
'description': u'Need to find a good Python tutorial on the web',
'done': False
}
]
@app.errorhandler(404)
def not_found(error):
return make_response(jsonify({'error': 'Not found'}), 404)
@app.errorhandler(400)
def bad_request(error):
return make_response(jsonify({'error': 'Bad request'}), 400)
@app.route('/todo/api/v1.0/tasks', methods=['GET'])
def get_tasks():
return jsonify({'tasks': tasks})
@app.route('/todo/api/v1.0/tasks/<int:task_id>', methods=['GET'])
def get_task(task_id):
task = [task for task in tasks if task['id'] == task_id]
if len(task) == 0:
abort(404)
return jsonify({'task': task[0]})
@app.route('/todo/api/v1.0/tasks', methods=['POST'])
def create_task():
if not request.json or not 'title' in request.json:
abort(400)
task = {
'id': tasks[-1]['id'] + 1,
'title': request.json['title'],
'description': request.json.get('description', ""),
'done': False
}
tasks.append(task)
return jsonify({'task', task}), 201
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
Thank you.
Upvotes: 4
Views: 5346
Reputation: 17078
As Pawel Kordowski mentioned above, you want to return a dictionary not a set:
return jsonify({'task': task}), 201
Upvotes: 4