'Python 3 Flask Rest Api: "request.get_json()" gives TypeError: 'NoneType' object is not subscriptable

I am creating a route in python flask which will server as rest api to register a user. when I try to get json data passed through postman in POST method, I get error TypeError: 'NoneType' object is not subscriptable

my request from postman: http://127.0.0.1:5000/register

raw input: {"username":"alok","password":"1234"}

my route and function:

@app.route('/register', methods=['GET', 'POST'])
def signup_user():
    data = request.get_json()
    return data['username']

As per my knowledge above function should return : "alok"

but i get error: TypeError: 'NoneType' object is not subscriptable

Any help will be appreciated



Solution 1:[1]

Add Content-Type : application/json to the header of your Post API request .

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tyler2P