Accessing the response
The response object is available on theHttpContext:
Sending responses
Implicit responses
Return data directly from route handlers:Explicit responses
Use response methods for more control:JSON responses
Send JSON data to the client:Status codes
Set HTTP status codes:Common status code examples
Response headers
Set response headers:Content type
Set the response content type:Cookies
Set and manage response cookies:Redirects
Redirect to a different URL:File downloads
Send files to the client:Streaming responses
Stream data to the client:Vary header
Set the Vary header for caching:ETags
Set ETag for caching:Aborting responses
Abort a response and send immediately:Getting response state
Check response state:Response macros
Extend the Response class with custom methods:TypeScript types
Define macro types:Best practices
Use appropriate status codes
Use appropriate status codes
Always use the correct HTTP status code for the response:
- 2xx for success
- 3xx for redirects
- 4xx for client errors
- 5xx for server errors
Set proper content types
Set proper content types
Always set the correct content type for your responses:
Use helper methods
Use helper methods
Use built-in helper methods instead of manually setting status codes:
Handle errors consistently
Handle errors consistently
Use a consistent error response format across your API:
Secure cookies properly
Secure cookies properly