17

I'm following the tutorial from FastAPI, and so far I have doubts about When/Where to use Body/Path/Query/Field in FastAPI? because all of them seem to work the same way, the tutorial uses vague explanations about their distinction, or am I missing something?

Bonus question: Is * really useful? I've set/omitted it in the sample code of tutorial, but I don't see the difference.

2

1 Answer 1

21

Actually, they are completely different.

Let's take the URL of questions tagged with FastAPI link as an example and split into parts.

https://stackoverflow.com/questions/tagged/fastapi?sort=Newest&uqlId=26120
  • stackoverflow.com -> domain
  • /questions -> Path
  • /tagged -> Path
  • /fastapi -> Path param.
  • sort=Newest -> Query Param.
  • uqlId=26120 -> Query Param.

If you wanted to create this in FastAPI it would look something like this.

from enum import Enum


class SortTypes(str, Enum):
    newest: str = "Newest"
    unanswered: str = "Unanswered"
    active: str = "Active"
    bountied: str = "Bountied"


@app.get("/questions/tagged/{tag}")
async def get_questions_with_tags(tag: str, sort: SortTypes, uqlId: int):
    return ...

Query params and path params work nearly the same.

But the body is completely different.

You can not see the body of the Request from URL, your client sent HTTP Body as bytes but it can contain anything, and you need to specify what body contains via HTTP Headers.

By doing that, you are telling the server how it should handle that body. For example

Imagine you are sending a JSON {"name": "foo"} and it will be sent with this header {"Content-Type": "application/json"}, your client handles this because by default FastAPI returns JSONResponse,there are also other response types like StreamingResponse, FileResponse, HTMLResponse etc (You can read common Content-Type's from here).

The main difference between Query params and the Path params is they are accessible from URL and they are strings. But Body is but and usually, it carries data.

Imagine you have a kinda bigger application and you are using Query params for all your communication between the client and the server. It will be a complete disaster.

For example, now you are reading this answer right? But how did it came? All the details your id, your reputation, the question itself comes as body. Imagine sending all this inside the URL, total disaster.

For further reading, you can check these answers to see Best Practices for designing a REST API.

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

3 Comments

Thanks @yagizcan-degirmenci, I understand better about queries and path parameters, and therefore QUERY, PATH, BODY, FIELD. I think you can complement your valuable explanation for Body with this call: curl -X PUT "http://127.0.0.1:8000/items4/345?q=holmes" -H "accept: application/json" -H "Content-Type: application/json" -d "{\"item\":{\"name\":\"string\",\"description\":\"string\",\"price\":0,\"tax\":0},\"user\":{\"username\":\"string\",\"full_name\":\"string\"},\"importance\":1}"
@ΟυιλιαμΑρκευα oh ofc, but the body can be any type, an image, a base64 encoded string, a JSON, or bytes object, etc. I will update my answer with a better explanation for the body. Thank you.
how to add a body parm into the endpoint definition too?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.