get_conditional_response
Evaluates HTTP request preconditions such as If-Match, If-Unmodified-Since, If-None-Match, and If-Modified-Since against provided entity tags and modification dates. It returns a conditional response (412 Precondition Failed or 304 Not Modified) if preconditions are not met, or the original response if they pass.
def get_conditional_response(
request: HttpRequest,
etag: str = None,
last_modified: datetime = None,
response: HttpResponse = None
) - > HttpResponse
Determines if a conditional HTTP response should be returned based on request headers and resource metadata. This function evaluates preconditions like If-Match and If-Modified-Since according to RFC 9110 to optimize bandwidth by returning '304 Not Modified' or '412 Precondition Failed' when appropriate.
Parameters
| Name | Type | Description |
|---|---|---|
| request | HttpRequest | The current HTTP request object containing the headers used for precondition evaluation. |
| etag | str = None | The entity tag representing the current version of the requested resource. |
| last_modified | datetime = None | The timestamp indicating when the resource was last changed. |
| response | HttpResponse = None | The initial response object to be returned if no conditional conditions apply. |
Returns
| Type | Description |
|---|---|
HttpResponse | A conditional response (304 or 412) if preconditions are met/failed, otherwise the original response object. |