2017-05-09 32 views
1

我有以下的燒瓶路線:瓶發帖JSON

@app.route('/change_groups', methods = ['POST']) 
def change_groups(): 
    if not request.json: 
    return "Not a json post" 
    return "json post!" 

當我嫋嫋喜歡我回去"Not a json post"預期:

curl --data "param1=value1&param2=value2" localhost:8000/change_groups 

然後我嘗試發佈JSON觸發"json post!"代碼:

curl -X POST -d '{"username":"xyz","password":"xyz"}' http://localhost:8000/change_groups 

第二次請求也觸發"Not a json post"當我被帶到cting "json post!"

我該如何similate張貼JSON到這個燒瓶路線?

回答

2

在你的要求,你不設置Content-Type: application/json,所以它不會被解析爲JSON。

1

找到答案很快,需要添加頭數據:

-H "Content-Type: application/json"