Managing requests and responses
Unlike in other frameworks, it is easy to capture the request body of the incoming POST, PUT, and PATCH request in Flask, which is through the get_json()
method from the request
proxy object. This utility method receives the incoming JSON data, parses the data using json.loads()
, and returns the data in a Python dictionary format. As seen in the following add_customer()
API, the value of get_json()
is converted into a kwargs
argument by Python’s **
operator before passing the request data to the model class’s constructor, an indication that the captured request data is a dict
convertible into kwargs
:
@current_app.post('/customer/add') def add_customer(): cust_json = request.get_json() repo = CustomerRepository(db_session) customer = Customer(**cust_json) ...