Tony Li
Tony Li

Reputation: 31

request.get_json() return 400 bad request

So I am testing POST to add new stuff to my database in postman and it keeps returning 400 error with no other info

127.0.0.1 - - [23/Feb/2021 00:46:04] "[1m[31mPOST /drinks HTTP/1.1[0m" 400 -

I tried to use request.is_json and it returns True. The code just returns error as long as it runs to

new_drink = request.get_json()

I have no idea where did I mess up.

@app.route('/drinks',methods=['POST'])
@requires_auth('post:drinks')
def add_drinks(payload):
    new_drink = request.get_json()
    title = new_drink.get('title')
    recipe = new_drink.get('recipe')
    drink = Drink(title=title,recipe=json.dumps(recipe))
    drink.insert()
    result = {
        "success": True,
        "drinks": drink.long()
    }
    return jsonify(result), 200

Here is a link to my project

https://github.com/TYL1026/coffee_shop/blob/main/backend/src/api.py

enter image description here

Upvotes: 1

Views: 1352

Answers (1)

arcsin
arcsin

Reputation: 461

As mentioned in the comments, in my case the problem was a header. I was setting "Content-Type": "application/json" but I was sending form-data. I removed the header and the problem was solved.

Upvotes: 1

Related Questions