Reputation: 33
I wrote the POST method in RESTful API of Flask and successfully added data to the server using HTTPie, a command line HTTP client. But when I tried it on Android there was an error. Here is the code of the client:
@app.route('/factories', methods=['POST'])
def create_factory():
if not request.json or not 'fac_name' in request.json:
abort(404)
factory = {
'id': factories[-1]['id'] + 1,
'fac_name': request.json['fac_name'],
'lat': request.json['lat'],
'lng': request.json['lng']
}
factories.append(factory)
return jsonify({'factory': factory}), 201
The HTTPie result is like this:
The error of Flask:
192.168.0.101 - - [13/May/2019 03:37:44] "POST /factories HTTP/1.1" 500 - Traceback (most recent call last):
File "C:\Users\Steve\PycharmProjects\test\venv\lib\site-packages\flask\app.py", line 2309, in call return self.wsgi_app(environ, start_response)
File "C:\Users\Steve\PycharmProjects\test\venv\lib\site-packages\flask\app.py", line 2295, in wsgi_app response = self.handle_exception(e)
File "C:\Users\Steve\PycharmProjects\test\venv\lib\site-packages\flask\app.py", line 1741, in handle_exception reraise(exc_type, exc_value, tb)
File "C:\Users\Steve\PycharmProjects\test\venv\lib\site-packages\flask_compat.py", line 35, in reraise raise value
File "C:\Users\Steve\PycharmProjects\test\venv\lib\site-packages\flask\app.py", line 2292, in wsgi_app response = self.full_dispatch_request()
File "C:\Users\Steve\PycharmProjects\test\venv\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request rv = self.handle_user_exception(e)
File "C:\Users\Steve\PycharmProjects\test\venv\lib\site-packages\flask\app.py", line 1718, in handle_user_exception reraise(exc_type, exc_value, tb)
File "C:\Users\Steve\PycharmProjects\test\venv\lib\site-packages\flask_compat.py", line 35, in reraise raise value
File "C:\Users\Steve\PycharmProjects\test\venv\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request rv = self.dispatch_request()
File "C:\Users\Steve\PycharmProjects\test\venv\lib\site-packages\flask\app.py", line 1799, in dispatch_request return self.view_functionsrule.endpoint
File "C:\Users\Steve\PycharmProjects\test\venv\hello.py", line 124, in create_factory
'fac_name': request.json['fac_name'],
TypeError: 'NoneType' object is not subscriptable
UPDATE:
I changed the if not
line into if not request.json: abort(401)
and now it is 401 error. And I also tried using Postman and it was the same type of error, so I think this has nothing to do with Android codes and i deleted the Android code from here.
Upvotes: 0
Views: 640
Reputation: 33
Instead of
if not request.json or not 'fac_name' in request.json:
abort(404)
factory = {
'id': factories[-1]['id'] + 1,
'fac_name': request.json['fac_name'],
'lat': request.json['lat'],
'lng': request.json['lng']
}
I used
data=request.data
j_data = json.loads(data)
factory = {
'id': factories[-1]['id'] + 1,
'fac_name': j_data['fac_name'],
'lat': j_data['lat'],
'lng': j_data['lng']
}
And it works. However I have no idea why.
Upvotes: 0
Reputation: 8052
The error is helpful.
File "C:\Users\Steve\PycharmProjects\test\venv\hello.py", line 124, in create_factory
'fac_name': request.json['fac_name'],
TypeError: 'NoneType' object is not subscriptable
object is not subscriptable
This means you are trying ['keyword']
on an object that does not support it, namely a NoneType
.
From which you can deduce request.json
is NoneType
Upvotes: 0
Reputation: 880
You are expecting a JSON request
if not request.json or not 'fac_name' in request.json:
But you are sending form-data.
Please check the Radio-Button "raw" and set the content Type to "application/json"
Then you have to add a valid json in your request body.
Upvotes: 1