在Flas中发布json时“400错误请求”

2024-05-23 17:36:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我的申请很简单

#@csrt.exempt
@app.route('/preorders/json', methods=['POST'])
def json_create_preorders():
    #print request
    print 'test'
    #print request.json
    print request.mimetype
    print request.json
    print 'aaa',request.get_json(force=True)
    print request.json['product_id']
    if not request.json or not 'product_id' in request.json or not 'customer_name' in request.json or not 'customer_phone' in request.json:
        abort(400)
    preorder=Preorder(request.json['customer_name'],request.json['customer_phone'],request.json['product_id'])
    db.session.add(preorder)
    db.session.commit()
    return jsonify({'status':'success'}), 201

使用curl发布json

curl -i -H "Content-Type: application/json" -X POST -d '{"product_id":"111", "customer_name"="xiaokun", "customer_phone"="1231"}' http://xxxx/preorders/json

从服务器检查,打印“test”和“request.mimetype”。然后是400个回复。 有人能帮忙看看吗?


Tags: ornameintestidjsonrequestnot
2条回答

试试这个

-d '{"product_id":"111", "customer_name":"xiaokun", "customer_phone":"1231"}'

完整语法

curl -X POST -H "application/json" -d '{"key":"val"}' URL

如果您是windows系统,则需要修改json格式。

示例:'{"token":"asdfas"}'替换为"{\"Hello\":\"Karl\"}"

相关问题 更多 >