使用JSON正文,而不是表单数据

时间:2018-07-26 09:11:15

标签: python

这是我的Python。输入为

curl http://35.173.47.24:8090/put -d {"id":102} -X PUT

我的代码:

from flask import Flask, request
from flask_restful import Resource, Api

app = Flask(__name__)
api = Api(app)

todos = {}
class TodoSimple(Resource):
    def put(self):
        i=request.form['id']
        print(i)
api.add_resource(TodoSimple, '/put')

if __name__ == '__main__':
    app.run(host='0.0.0.0',port=8090,debug=True)

我需要传递json参数。我必须更改什么?

2 个答案:

答案 0 :(得分:1)

将请求更改为

curl -X PUT http://25.173.47.24:8080/put -d '{"id":102}' -H "Content-Type:application/json"

答案 1 :(得分:0)

问题已解决,与代码有关。应该使用request.get_json