FlukyRaccker
FlukyRaccker

Reputation: 3

Flask gettin 'NoneType' object is not subscriptable

Im making an Api that get two datas, "id" and "name", but i'm getting 'NoneType' object is not subscriptable" erro in name = request.json['Name']

from flask import Flask, jsonify, request, Response #import flask library

from checking import checker


app = Flask(__name__)

@app.route("/v1/products", methods=["GET", "POST"])
def getData():
    user_id = request.json['id']
    name = request.json['Name']
    data = {'id' : user_id, 'Name' : name}
    flag =  checker(data)

    if flag == True:
        return 200, 'OK'
    else:
        return 403, 'Forbidden'

    


if __name__ == '__main__':
    app.run(host='localhost', debug=True)

To send the data for API, i run the follow code:

curl -X POST -H "Content-Type: v1/products" -d '{'id' : '123', 'Name' : 'mesa'}' http://localhost:5000/v1/products

What i'm doing wrong ?

Upvotes: 0

Views: 426

Answers (2)

Kendal Yorke
Kendal Yorke

Reputation: 56

This seem to work for me. Hope it does for you:

from flask import Flask, jsonify, request, Response #import flask library

app = Flask(__name__)


@app.route("/v1/products", methods=["GET", "POST"])
def getData():
    user_id = request.args['id']
    name = request.args['Name']
    data = {'id' : user_id, 'Name' : name}
    print(data)

    if request.args != None:
        print('OK')
    else:
        print('Forbidden')

    return (data)


if __name__ == '__main__':
    app.run(host='localhost', debug=True)

Upvotes: 0

kerasbaz
kerasbaz

Reputation: 1794

The issue isn't with your flask code but with your curl request. You aren't setting the content type and body properly, so there is no json for your API endpoint to process.

You'll want to change the first part of your command to: curl -X POST -H "Content-Type: application/json"

You may also have issues with your quotes in the request body, you'll either need to escape or modify the quotes so they aren't ambiguous.

Upvotes: 1

Related Questions