Bird
0
0

How can you customize the HTTP response headers when raising an HTTPException in FastAPI?

hard🚀 Application Q9 of 15
FastAPI - Error Handling
How can you customize the HTTP response headers when raising an HTTPException in FastAPI?
ASet headers directly on the response object before raising HTTPException
BPass a headers dictionary as the headers parameter in HTTPException
CInclude headers in the detail parameter as a dict
DHeaders cannot be customized with HTTPException
Step-by-Step Solution
Solution:
  1. Step 1: Recall HTTPException parameters

    HTTPException accepts a headers parameter to customize response headers.
  2. Step 2: Evaluate other options

    Setting headers on response before raising is not effective; detail is for message; headers can be customized.
  3. Final Answer:

    Pass a headers dictionary as the headers parameter in HTTPException -> Option B
  4. Quick Check:

    Use headers param in HTTPException to customize headers [OK]
Quick Trick: Use headers param in HTTPException to add custom headers [OK]
Common Mistakes:
MISTAKES
  • Trying to set headers on response object before raising
  • Putting headers inside detail message
  • Assuming headers customization is not possible

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes