I am learning python flask and was writing some primitive codes to know more about it.
I found that print(api.payload) and print(request.get_json()) print the same output
i.e it returns the json file contents as expected(whatever string I pass as input on the IP 127.0.0.1:5000/).
In this case I executed a Post request with contents {'Make': 'toyota'}
127.0.0.1 - - [12/Oct/2020 15:59:26] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [12/Oct/2020 15:59:26] "GET /swagger.json HTTP/1.1" 200 -
127.0.0.1 - - [12/Oct/2020 15:59:26] "GET /swaggerui/favicon-32x32.png HTTP/1.1" 200 -
{'Make': 'toyota'}
{'Make': 'toyota'}
The example code used:
from flask import Flask, request
from flask_restplus import Api, Resource, fields
app = Flask(__name__)
api = Api(app)
car_model = api.model('Car Info', {'Make' : fields.String('The car company')})
car = {'Make' : 'Honda'}
@api.route('/home')
class Car(Resource):
def get(self):
return car
@api.expect(car_model)
def post(self):
print(request.get_json())
print(api.payload)
return {'Result' : 'Success'}, 200
if __name__ == '__main__':
app.run(debug=True)
Can anyone tell me if there are any advantages of using one over another or both can be used interchangeably? If there are any advantages what are they and why one should be used over the other?