0

The request for an ID does return a record correctly, but the URL has the record being searched in front of a "/" in the address bar:

http://fastapi.localhost:8008/person/2

Below is my request paramter for a GET request:

@app.get("/person/{id}", tags=["API Endpoint"])
async def get_persons(id: int | None = None, person: str | None = None, yesno: str | None = None):
    id_list = await Person.objects.get(pk=id)
    return id_list

And the response_model is structured as follows:

class Person(ormar.Model):
class Meta(BaseMeta):
    tablename = str = "persons"

id: int = ormar.Integer(primary_key=True)
person: str = ormar.String(max_length=250, nullable=True)
yesno: str = ormar.String(max_length=4, nullable=True)

Ideally, the URL would have the equal sign:

http://fastapi.localhost:8008/person=2

Please let me know if there's any additional information I can provide and thank you in advance.

IamTrying
  • 39
  • 6
  • 2
    `/person/2` is the conventional way to request the person with id 2 in a REST API, and when you say `/person/{id}`, you're configuring your API to _expect_ that `/`. The only thing that's wrong here is the idea that it should be `=`. – Charles Duffy Dec 15 '22 at 19:00
  • 1
    Short answer is because you set it like that in the route you defined, to use it with `=` you must set it as a query parameter and would be like `?person=2` but it will be strange to do it like that – Tony Dec 15 '22 at 19:04
  • @CharlesDuffy thank you for your reply and understood. In the event you wanted to filter on a person's last name, if the person field was broken into a first and last name field, and I had an endpoint `/person/{last_name}`. Assuming there are more than one person with the same last name, would you then expect to see something like `/person/{last_name}/{first_name}`? Or would you expect something like `/person/{last_name}?first_name="some name"`? Additionally, if you have any reading you'd recommend for API design, that'd be greatly appreciated. First timer here... – IamTrying Dec 15 '22 at 21:22
  • 2
    The expected API format (for me) in the filtering case would usually be `/persons?last_name=&first_name=` - i.e. you're requesting items from the `/persons` resource, filtered by the arguments given in the URL. When looking up a single object/resource, `/persons/` is what I find most natural. That way the hierarchy matches "under persons, theres a given person with a given id". – MatsLindh Dec 16 '22 at 11:06

0 Answers0