Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Flask-Restful POST not taking JSON arguments

I am very new to Flask (& Flask-Restful).

My problem : json arguments for a POST is getting set to NONE (not working).

I am able to take arguments from the form-data, using POSTMAN plugin for chrome. But, when i switch to raw (& feed a json), it fails to read the json & assigns a NONE to all my arguments.

I have read some related stackoverflow posts related to this : link1, link2, link3 ... none of these helped me.

I am using python-2.6, Flask-Restful-0.3.3, Flask-0.10.1, Chrome, POSTMAN on Oracle Linux 6.5.

Python code app.py :

from flask import Flask, jsonify from flask_restful import reqparse, abort, Api, Resource  app = Flask(__name__) api = Api(app)  parser = reqparse.RequestParser() parser.add_argument('username', type=str) parser.add_argument('password', type=str)  class HelloWorld(Resource):     def post(self):         args = parser.parse_args()         un = str(args['username'])         pw = str(args['password'])         return jsonify(u=un, p=pw)  api.add_resource(HelloWorld, '/testing')  if __name__ == '__main__':     app.run(host='0.0.0.0', port=5444 ,debug=True) 

Testing this using POSTMAN :

  • Using form-data : works perfectly !
  • Using raw -> json : causes this issue

Things tried #1 :

Add json parameter to my add_argument() method in app.py

parser = reqparse.RequestParser() parser.add_argument('username', type=str, location='json') # added json parser.add_argument('password', type=str, location='json') # added json 

Input : { "username": "hello", "password": "world" }

Output : { "p": "None", "u": "None" }

Things tried #2 :

Change type to unicode in add_argument() method in app.py

parser = reqparse.RequestParser() parser.add_argument('username', type=unicode, location='json') # change type to unicode parser.add_argument('password', type=unicode, location='json') # change type to unicode 

Input : { "username": "hello", "password": "world" }

Output : { "p": "None", "u": "None" }


PS : I will keep updating my question, with every failed attempt. Please let me know if you need any more info to make this question more clear.

like image 960
sudhishkr Avatar asked May 27 '15 19:05

sudhishkr


People also ask

How do I get post JSON data in Flask?

You need to set the request content type to application/json for the . json property and . get_json() method (with no arguments) to work as either will produce None otherwise.

Is Flask-RESTful deprecated?

The whole request parser part of Flask-RESTful is slated for removal and will be replaced by documentation on how to integrate with other packages that do the input/output stuff better (such as marshmallow). This means that it will be maintained until 2.0 but consider it deprecated.

Is Flask-RESTful maintained?

update When reading this accepted answer, consider that there is Flask-RESTX which is a fork of Flask-RESTPlus that is maintained, as an alternative option.


1 Answers

junnytony's answer gave me a hint, and I went ahead with this approach. get_json seems to have done the trick.

from flask import Flask, jsonify, request from flask_restful import reqparse, abort, Api, Resource  app = Flask(__name__) api = Api(app)  #parser = reqparse.RequestParser() #parser.add_argument('username', type=unicode, location='json') #parser.add_argument('password', type=unicode, location='json')  class HelloWorld(Resource):     def post(self):         json_data = request.get_json(force=True)         un = json_data['username']         pw = json_data['password']         #args = parser.parse_args()         #un = str(args['username'])         #pw = str(args['password'])         return jsonify(u=un, p=pw)  api.add_resource(HelloWorld, '/testing')  if __name__ == '__main__':     app.run(host='0.0.0.0', port=5444 ,debug=True) 
like image 76
sudhishkr Avatar answered Sep 20 '22 19:09

sudhishkr