Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Flask REST API - parameter parsing

from flask import Flask
from flask_restful import Api, Resource, reqparse

app = Flask(__name__)
api = Api(app)

class Echo(Resource):
    #def get(self):
    #    parser = reqparse.RequestParser()
    #    parser.add_argument('text', type=str, help='Type in some text')
    #    args = parser.parse_args()
    #    text = args.get('text', '')
    #    return { "res": f"Text: {text}" }

    def post(self):
        print("in post")
        parser = reqparse.RequestParser()
        # curl -H "Content-Type: application/json" -d '{"text": 23}' -i http://localhost:5000/echo
        #parser.add_argument('text', type=int, help='Type in some text')

        # curl -H "Content-Type: application/json" -d '{"text": "hello world"}' -i http://localhost:5000/echo
        parser.add_argument('text', help='Type in some text')
        args = parser.parse_args()
        print("parsed")
        return { "Answer": f"You said: {args['text']}" }

class Area(Resource):
    def post(self):
        parser = reqparse.RequestParser()
        # $ curl -H "Content-Type: application/json" -d '{"width": 3, "height": 4}' -i http://localhost:5000/area
        parser.add_argument('width', type=int, help='integer')
        parser.add_argument('height', type=int, help='integer')
        args = parser.parse_args()
        width = args.get('width', 0)
        height = args.get('height', 0)
        area = width * height
        return { "area": area }


api.add_resource(Echo, '/echo')
api.add_resource(Area, '/area')

import api
import pytest

@pytest.fixture()
def web():
    return api.app.test_client()

#def test_echo_get_with_param(web):
#    rv = web.get('/echo?text=hello')
#    assert rv.status == '200 OK'
#    assert rv.headers['Content-Type'] == 'application/json'
#    assert rv.json ==  {'res': 'Text: hello'}

#def test_echo_post_with_param(web):
#    rv = web.post('/echo', data={'text': 'ciao'}, headers={'Content-Type':'application/json'})
#    assert rv.status == '200 OK'
#    assert rv.headers['Content-Type'] == 'application/json'
#    assert rv.json == {'Answer': 'You said: ciao'}


def test_echo_bad_request_content_type(web):
    rv = web.post('/echo')
    assert rv.status == '415 UNSUPPORTED MEDIA TYPE'
    assert rv.headers['Content-Type'] == 'application/json'
    assert rv.json == {'message': "Did not attempt to load JSON data because the request Content-Type was not 'application/json'."}

#def test_echo_get_no_param(web):
#    # If the parameter is missing the parser just returns None
#    rv = web.get('/echo')
#    assert rv.status == '200 OK'
#    assert rv.headers['Content-Type'] == 'application/json'
#    assert rv.json ==  {'res': 'Text: None'}

#def test_echo_post_no_param(web):
#    rv = web.post('/echo')
#    assert rv.status == '200 OK'
#    assert rv.headers['Content-Type'] == 'application/json'
#    assert rv.json == {'Answer': 'You said: ciao'}