PacktPublishing / Building-Data-Science-Applications-with-FastAPI

Building Data Science Applications with FastAPI, Published by Packt
MIT License
306 stars 157 forks source link

Chapter3_headers_cookies_02.py calling from automatic docs forces user-agent entry that is ignored #7

Open gitgithan opened 2 years ago

gitgithan commented 2 years ago

The book demonstrates calling using httpie, so i wanted to test calling from the docs page since it provides convenience in

However I am unable to leave it empty. If i type gibberish, then it gives in response

{
  "user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)...
}

Question Where is the validation on the docs defined? It seems like a separate system from fastapi? When things like Query(...,ge=0) are defined and you enter -1, the docs do not even allow Execute to be clicked, showing a red background in the textbox. What is causing this? I also noticed in chapter5_function_dependency_02 if skip: int = Query(0, ge=0) was changed to skip: int = Query(0, gt=0), minimum: 0 will disappear from the docs, how does this happen?

What is the difference between calling from httpie, docs or other tools?

frankie567 commented 2 years ago

The interactive API docs is based on Swagger, which is an open source tool generating this JavaScript interface from an OpenAPI specification. This OpenAPI specification is automatically generated by FastAPI from the constraint you define in Query and other dependencies. You can actually access it from http://localhost:8000/openapi.json.

To help you interact with your API, Swagger automatically adds browser validation on the parameters from this specification. This is why the interface doesn't allow you to trigger the request if the parameter is not valid.

This is why I prefer to recommend HTTPie, because it has no hidden magic: it lets you make the query exactly as you like and then see what the server has to answer to this request.