Skip to main content

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

NameTypeDescription
requestHttpRequestThe current HTTP request object containing the headers used for precondition evaluation.
etagstr = NoneThe entity tag representing the current version of the requested resource.
last_modifieddatetime = NoneThe timestamp indicating when the resource was last changed.
responseHttpResponse = NoneThe initial response object to be returned if no conditional conditions apply.

Returns

TypeDescription
HttpResponseA conditional response (304 or 412) if preconditions are met/failed, otherwise the original response object.