0

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:

gist

6
  • what do you get when you print(repr(var_name)) right before you return the error message? Commented Jun 28, 2024 at 10:17
  • also, I think you meant @app.route("/person/<uuid:var_name>") Commented Jun 28, 2024 at 10:20
  • tried that: does not work or returns anything Commented Jun 28, 2024 at 10:30
  • but when I detete the 2nd return it works and returns person... Commented Jun 28, 2024 at 10:31
  • I think I found the root of your problem: when you curl /person without a uuid, flask doesn't recognize it as /person/<var_name>, so it doesn't find a route Commented Jun 28, 2024 at 13:30

2 Answers 2

0

your for loop isn't actually looping through your data, since you are always returning in the first iteration. Putting the return {"error_message: ..."}, 404 after the loop should make it work.

Sign up to request clarification or add additional context in comments.

5 Comments

that is what I did first: not having a "else:"
if I do that, the error message comes back in HTML and not in json as I implemented in the code
like a default error message
@AlanBitts returning the error inside the loop is definitely a logic error. Also notice that I can't reproduce any of your described issues with the code that you provided, so you clearly left something out which is important to the issues
I edited my original question and provided a Gist link to my API code
0

Flask automatically returns an HTML response as default 404 error message when I do not enter a valid request parameter.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.