Flask-restful API not accepting json
<p>I am trying to learn how to do apis. I copied everything from the book exactly but i am unable to post to the api.  I tried posting <code>{'name':'holy'}</code> as raw data in <code>postman</code>( an json posting tool) to api and I get the vladation help message error"No Name provided":but when i try <code>name=holy</code> it works fine. I thought it was not suppose to work like that, How do i get it to work with <code>{'name':'holy'}</code></p>

<pre><code>from flask import Flask, request,render_template, jsonify
from flask_restful import Resource, Api,marshal_with, fields, reqparse

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


class UserApi(Resource):
    def __init__(self):
        self.reqparse = reqparse.RequestParser()
        self.reqparse.add_argument(
            'name',
            required=True,
            help='No name provided',
            location=['form', 'json']
        )

    def get(self):
        return jsonify ({"first_name":"Holy","last_name": "Johnson"})

    def post(self):
        args = self.reqparse.parse_args()
        return jsonify ({"first_name":"Holy","last_name": "Johnson"})



api.add_resource(UserApi, '/users')

if __name__ == '__main__':
    app.run(debug=True)
</code></pre>

<p>Your code should work - you have to specify the request header <code>Content-Type: application/json</code>. The reason why is because <code>flask-restful</code>'s <code>reqparse</code> module tries to parse <a href="http://flask-restful-cn.readthedocs.io/en/0.3.5/reqparse.html#argument-locations" rel="nofollow">its data from <code>flask.request.json</code></a>, which is only set if <code>Content-Type: application/json</code> is set.</p>

<p>If you have access to <code>curl</code> (or <code>wget</code>), you can do the following to test:</p>

<pre><code>$shell&gt; curl -X POST -H "Content-Type: application/json" -d '{"name": "holly"}' http://localhost:5000/users
{
  "first_name": "Holy",
  "last_name": "Johnson"
}
</code></pre>

<p>In Postman, you can set a header, like shown in the screenshot below. <a href="https://i.stack.imgur.com/3nS91.png" rel="nofollow"><img src="https://i.stack.imgur.com/3nS91.png" alt="enter image description here"></a></p>

python rest api flask-restful