Adding Create, Update, and Delete operations to our service (Flask version)

We want to add the following routes to our service:

By default, routes are triggered by 'GET' actions. But @app.route accepts a methods parameter where you can list the expected HTTP actions. Next you can learn which action from the list was requested by using request.method.
example:

You can easily access the JSON content of the HTTP request body part using request.get_json().

To add headers to a response, create a Flask.Response and list headers as a dictionary:

Modify your code to include the new routes. Test locally using RESTer (or add the -i option to curl to visualize not only the response body but also the code and the headers).

Once the code runs fine locally, deploy it to pythonanywhere.

Expected result:

curl -i http://localhost:5000/members
HTTP/1.1 200 OK
Server: Werkzeug/3.0.1 Python/3.10.12
Date: Wed, 20 Mar 2024 10:35:59 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 152
Connection: close


[]

curl http://localhost:5000/members/FZ9
404 NOT FOUND
Unknown license number

curl -X POST http://localhost:5000/members \
-H 'Content-Type: application/json' \
-d '{
"birthYear": 2010,
"gender": "M",
"name": "John"
}'

201 CREATED
Location: /members/MJ1
new member added

curl http://localhost:5000/members
200 OK
[{"birthYear": 2010, "gender": "M", "name": "John", "licenseNumber": "MJ1"}]

curl -X POST http://localhost:5000/members \
-H 'Content-Type: application/json' \
-d '{
"birthYear": 2015,
"gender": "F",
"name": "Mary"
}'

201 CREATED
Location: /members/FM2
new member added

curl http://localhost:5000/members
200 OK
[{"birthYear": 2010, "gender": "J", "name": "John", "licenseNumber": "MJ1"}, {"birthYear": 2015, "gender": "F", "name": "Mary", "licenseNumber": "FM2"}]

curl 'http://localhost:5000/membersInRange?ageMin=20&ageMax=25'
400 BAD REQUEST
Bad Request: Missing 'minAge' or 'maxAge' parameter

curl 'http://localhost:5000/membersInRange?minAge=10&maxAge=25'
200 OK
[{"birthYear": 2010, "gender": "J", "name": "John", "licenseNumber": "MJ1"}]

curl http://localhost:5000/members/FM2
200 OK
{
"birthYear": 2015,
"gender": "F",
"licenseNumber": "FM2",
"name": "Mary"
}

curl -X DELETE http://localhost:5000/members/MJ1
204 NO CONTENT

curl http://localhost:5000/members
200 OK
[{"birthYear": 2015, "gender": "F", "name": "Mary", "licenseNumber": "FM2"}]

curl -X DELETE http://localhost:5000/members/MJ1
404 NOT FOUND
Unknown license number

curl -X PUT http://localhost:5000/members/FM2 \
-H 'Content-Type: application/json' \
-d '{
"name": "Mary-Lou"
}'

204 NO CONTENT

curl http://localhost:5000/members/FM2
200 OK
{
"birthYear": 2015,
"gender": "F",
"licenseNumber": "FM2",
"name": "Mary-Lou"
}

curl -X DELETE http://localhost:5000/members
204 NO CONTENT

curl http://localhost:5000/members
200 OK
[]