无法发送POST请求到我的基本FLASK Python应用程序
问题描述:
我想在Python中使用Flask框架在我简单的rest api中测试基本的HTTP请求。 GET方法工作得很好,而不是POST方法。这里是路线:无法发送POST请求到我的基本FLASK Python应用程序
@app.route('/lang', methods=['POST'])
def addOne():
language = {'name' : request.json['name']}
languages.append(language)
return jsonify({'languages' : languages})
我的语言词典:
languages = [{'name' : 'JavaScript'},{'name' : 'Java'}, {'name' : 'Python'}]
我试图用邮差的应用程序来发布新的语言的字典,这里是要求:
http://127.0.0.1:8080/lang
而在体内,我把这一行放在这里:
{"name" : "C++"}
它给我这个错误:
File "/home/pi/IoT_api/restful.py", line 22, in addOne
language = {'name' : request.json['name']}
TypeError: 'NoneType' object has no attribute '__getitem__'
答
的documentation明确表示:
If the mimetype is application/json this will contain the parsed JSON data. Otherwise this will be None.
所以一定要与Postman
正确定义 '的Content-Type' 头你告诉邮递员发送JSON内容类型标题? –
@DanielRoseman是 – Hussein
您发送了哪些标题? –