When trying to curl this endpoint with curl -X GET -i localhost:5000/person/66c09925-589a-43b6-9a5d-d1601cf53287
the implementation works fine:
HTTP/1.1 200 OK Server: Werkzeug/3.0.3 Python/3.12.3 Date: Fri, 28 Jun 2024 13:19:49 GMT Content-Type: application/json Content-Length: 294 Connection: close { "address": "637 Carey Pass", "avatar": "http://dummyimage.com/174x100.png/ff4444/ffffff", "city": "Gainesville", "country": "United States", "first_name": "Lilla", "graduation_year": 1985, "id": "66c09925-589a-43b6-9a5d-d1601cf53287", "last_name": "Aupol", "zip": "32627" } but when I purposedly make a wrong request the server returns the error in HTML instead of JSON like I'm trying to implement on the /person endpoint
HTTP/1.1 404 NOT FOUND Server: Werkzeug/3.0.3 Python/3.12.3 Date: Fri, 28 Jun 2024 13:02:53 GMT Content-Type: text/html; charset=utf-8 Content-Length: 207 Connection: close <!doctype html> <html lang=en> <title>404 Not Found</title> <h1>Not Found</h1> <p>The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.</p> I want to return {"error_message":"person not found"}, 404
This is my endpoint in server.py with a hardcoded list
from flask import Flask, make_response, request app = Flask(__name__) data = [ { "id": "66c09925-589a-43b6-9a5d-d1601cf53287", "first_name": "Lilla", "last_name": "Aupol", "graduation_year": 1985, "address": "637 Carey Pass", "city": "Gainesville", "zip": "32627", "country": "United States", "avatar": "http://dummyimage.com/174x100.png/ff4444/ffffff", } ] @app.route("/person/<var_name>") def find_by_uuid(var_name): for person in data: if person["id"] == str(var_name): return person else: return {"error_message":"person not found"}, 404 I tried to use the type:name syntax to only allow callers to pass in a valid UUID.
like this:
@app.route("/person/<type:var_name>") the following is the full code from server.py:
print(repr(var_name))right before you return the error message?@app.route("/person/<uuid:var_name>")/personwithout a uuid, flask doesn't recognize it as/person/<var_name>, so it doesn't find a route