Response Object
NexiosResponse
Class¶
The NexiosResponse
class is a fluent interface for creating and customizing HTTP responses in a Nexio application. It supports various response types, including plain text, JSON, HTML, files, streaming, and redirects. It also provides methods for setting headers, cookies, and caching behavior.
The NexiosResponse class is a fluent interface and abstraction layer that simplifies the creation and customization of HTTP responses in a Nexio application. Instead of directly instantiating specific response classes like Response, JSONResponse, FileResponse, etc., developers can use NexiosResponse to handle all response types in a consistent and intuitive way.
Methods¶
send(content: Any, status_code: Optional[int] = None, headers: Dict[str, Any] = {})
¶
Sends a plain text or HTML response.
- Parameters:
content
: The response body (can be any type).status_code
: The HTTP status code (default:200
).headers
: Additional headers to include in the response.- Returns:
self
(for method chaining). - Example:
text(content, status_code,headers)
¶
Sends a plain text response.
- Parameters:
content
: The response body (can bestr
,dict
,list
, etc.).status_code
: The HTTP status code (default:200
).headers
: Additional headers to include in the response.- Returns:
self
(for method chaining). - Example:
json(content, status_code,headers)
¶
Sends a JSON response.
- Parameters:
data
: The JSON-serializable data to send.status_code
: The HTTP status code (default:200
).headers
: Additional headers to include in the response.- Returns:
self
(for method chaining). - Example:
empty(status_code headers)
¶
Sends an empty response with no body.
- Parameters:
status_code
: The HTTP status code (default:200
).headers
: Additional headers to include in the response.- Returns:
self
(for method chaining). - Example:
html(content, status_code ,headers)
¶
Sends an HTML response.
- Parameters:
content
: The HTML content to send.status_code
: The HTTP status code (default:200
).headers
: Additional headers to include in the response.- Returns:
self
(for method chaining). - Example:
file(path, filename, content_disposition_type)
¶
Sends a file as a response.
- Parameters:
path
: The path to the file on the server.filename
: The name of the file to send (default: the file's basename).content_disposition_type
: The type of content disposition ("attachment"
or"inline"
).- Returns:
self
(for method chaining). - Example:
stream(iterator, content_type)
¶
Sends a streaming response.
- Parameters:
iterator
: An iterator that yields chunks of data (str
orbytes
).content_type
: The content type of the stream (default:"text/plain"
).- Returns:
self
(for method chaining). - Example:
redirect(url ,status_code)
¶
Sends a redirect response.
- Parameters:
url
: The URL to redirect to.status_code
: The HTTP status code (default:302
).- Returns:
self
(for method chaining). - Example:
status(status_code)
¶
Sets the HTTP status code for the response.
- Parameters:
status_code
: The HTTP status code.- Returns:
self
(for method chaining). - Example:
header(key, value)
¶
Sets a header in the response.
- Parameters:
key
: The header name.value
: The header value.- Returns:
self
(for method chaining). - Example:
set_cookie(key, value , max_age, expires, path, domain, secure, httponly, samesite)
¶
Sets a cookie in the response.
- Parameters:
key
: The cookie name.value
: The cookie value.max_age
: The maximum age of the cookie in seconds.expires
: The expiration date of the cookie.path
: The path for the cookie (default:"/"
).domain
: The domain for the cookie.secure
: Whether the cookie is secure (default:True
).httponly
: Whether the cookie is HTTP-only (default:False
).samesite
: TheSameSite
attribute for the cookie.- Returns:
self
(for method chaining). - Example:
delete_cookie(key, value , max_age, expires, path, domain, secure, httponly, samesite)
¶
Deletes a cookie by setting its expiration to the past.
- Parameters:
key
: The cookie name.value
: The cookie value (optional).max_age
: The maximum age of the cookie in seconds (optional).expires
: The expiration date of the cookie (optional).path
: The path for the cookie (default:"/"
).domain
: The domain for the cookie (optional).secure
: Whether the cookie is secure (default:False
).httponly
: Whether the cookie is HTTP-only (default:False
).samesite
: TheSameSite
attribute for the cookie (optional).- Returns:
self
(for method chaining). - Example:
cache(max_age, private)
¶
Enables caching for the response.
- Parameters:
max_age
: The maximum age of the cache in seconds (default:3600
).private
: Whether the cache is private (default:True
).- Returns:
self
(for method chaining). - Example:
no_cache()
¶
Disables caching for the response.
- Returns:
self
(for method chaining). - Example: