We want to add the following routes to our service:
DELETE /members
: remove all members (and reset the number of members), returns HTTP response code 204 ("No content")DELETE /members/{ln}
: remove a specific member, whose license number is given in the url, returns HTTP response code 204 or 404 if the specified member was not foundPOST /members
: expect a json body such as below, add the new member and return HTTP response code 201 ("Created"), with a "Location" header that indicates the URL of the newly created member (example: /member/FE4, "FE4" will be the license number given to the new member, it's made up with the gender ("M"), the first letter of the name ("E") and the total number of members added since the last call to DELETE{ "birthYear": 2005, "gender": "F", "name": "Emma" }
PUT /members/{ln}
: expect a json body with the new name, update the member's name and return HTTP response code 204 ("No content"), or 404 if the specified member was not found. Note that sometimes REST services return a 200 ("OK") code with the updated description of the resource.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
[]