Data Mastery
Data Mastery

Reputation: 2085

POST request with Flask Restful leads to TypeError

from flask import Flask, request
from flask_restful import Resource, Api

app = Flask(__name__)
api = Api(app)

items = []

class Item(Resource):

    def post(self, name):
        data = request.get_json()
        item = {'name': name, 'price': data['price']}
        items.append(item)
        return item

api.add_resource(Item, "/item/<string:name>")


app.run(port=5000, debug=True)

This is my code. Trying to make a post request with Postman:

http://127.0.0.1:5000/item/chair

This is the body:

{
    "price": 15.99
}

When doing the Post request, I get the following error:

TypeError: 'NoneType' object is not subscriptable

Why does my data result in this? Can anyone help me out?

Upvotes: 1

Views: 323

Answers (2)

atwalsh
atwalsh

Reputation: 3722

Make sure you configure the Content-Type header of your request to be application/json. Flask's Request.get_json() method will return None if the Content-Type of your request mimetype does not indicate JSON.

See the Postman docs on configuring request headers.

Upvotes: 1

Jonathan Holmes
Jonathan Holmes

Reputation: 468

Your issue is that your POST request doesn't properly fill out its headers. A quick test with CURL demonstrates this:

vagrant@vagrant:~$ curl -d '{"price":15.99}' -H "Content-Type: application/json" -X POST http://localhost:5000/item/chair
{
    "name": "chair",
    "price": 15.99
}
vagrant@vagrant:~$ curl -d '{"price":15.99}' -X POST http://localhost:5000/item/chair
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <title>TypeError: 'NoneType' object has no attribute '__getitem__' // Werkzeug Debugger</title>
    <link rel="stylesheet" href="?__debugger__=yes&amp;cmd=resource&amp;f=style.css"
        type="text/css">
    <!-- We need to make sure this has a favicon so that the debugger does
         not by accident trigger a request to /favicon.ico which might
         change the application state. -->
    <link rel="shortcut icon"
        href="?__debugger__=yes&amp;cmd=resource&amp;f=console.png">
    <script src="?__debugger__=yes&amp;cmd=resource&amp;f=jquery.js"></script>
    <script src="?__debugger__=yes&amp;cmd=resource&amp;f=debugger.js"></script>
    <script type="text/javascript">
      var TRACEBACK = 140264881526352,
          CONSOLE_MODE = false,
...

I cut out the rest of the HTML for brevity. Nothing is wrong with your code; you need to specify the `Content-Type: application/json" header when you make your Postman request.

Upvotes: 1

Related Questions