' 400错误请求'当在烧瓶中发布json时

时间:2015-04-11 10:12:37

标签: python json flask flask-restful

我的申请很简单,

#@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

POST json with curl,

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响应。 任何人都可以帮忙看看吗?

2 个答案:

答案 0 :(得分:9)

试试这个

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

完整语法

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

答案 1 :(得分:7)

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

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