Skip to Content
Flask Framework Cookbook - Second Edition
book

Flask Framework Cookbook - Second Edition

by Shalabh Aggarwal
July 2019
Beginner to intermediate
302 pages
9h 38m
English
Packt Publishing
Content preview from Flask Framework Cookbook - Second Edition

How to do it...

The following lines of code are the complete RESTful API for the Product model. These code snippets will go into the views.py file.

Start with imports and add parser:

import jsonfrom flask import abortfrom flask_restful import reqparse 
 
parser = reqparse.RequestParser() 
parser.add_argument('name', type=str) 
parser.add_argument('price', type=float) 
parser.add_argument('category', type=dict) 

In the preceding snippet, we created parser for the arguments that we expected to have in our requests for POST and PUT. The request expects each of the arguments to have a value. If a value is missing for any argument, then None is used as the value.

Write the method as shown in the following code block to fetch products:

class ProductApi(Resource): ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Writing a Web Application with Flask

Writing a Web Application with Flask

Doug Farrell
Mastering Flask

Mastering Flask

Jack Stouffer

Publisher Resources

ISBN: 9781789951295Supplemental Content