Flask REST API - parameters in path



examples/flask/api_path/api.py
from flask import Flask, request
from flask_restful import Api, Resource

app = Flask(__name__)

api = Api(app)

class Echo(Resource):
    def get(self, me):
        return { "res": f"Text: {me}" }

    def post(self, me):
        return { "Answer": f"You said: {me}" }


api.add_resource(Echo, '/echo/<me>')

examples/flask/api_path/test_api.py
import api

def test_echo():
    web = api.app.test_client()

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


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