Choosing the appropriate responses
The FastAPI framework offers other options for rendering API endpoint responses other than the most common JsonResponse
option. Here is a list of some of the response types supported by FastAPI and their corresponding samples from our application:
- The API endpoints can utilize the
PlainTextResponse
type if their response is text-based only. The followingintro_list_restaurants()
service returns a text-based message to the client:@router.get("/restaurant/index") def intro_list_restaurants(): return PlainTextResponse(content="The Restaurants")
- Services can use
RedirectResponse
if they need to pursue navigation to another entirely different application or another endpoint of the same application. The following endpoint jumps to a hypertext reference about some known Michelin-starred restaurants:@router.get("/restaurant/michelin") def redirect_restaurants_rates(): return RedirectResponse...