HTTP Utilities¶
These helpers simplify working with request metadata and standardized API responses.
Module drf_corekit.http.requests¶
Utilities for extracting information from incoming requests.
get_current_site_info(request)¶
Returns site metadata for the current request.
Returns¶
Behavior¶
- Protocol is:
- http if
DEBUG=True - https otherwise
Example¶
get_client_ip(request)¶
Extracts the client IP address, respecting common proxy headers.
Priority¶
HTTP_CF_CONNECTING_IPHTTP_TRUE_CLIENT_IPHTTP_X_FORWARDED_FOR(first valid IP)HTTP_X_REAL_IPHTTP_FORWARDED(for=token)REMOTE_ADDR
Returns¶
Example¶
get_request_txid(request)¶
Retrieves request transaction ID if available.
This assumes a middleware has attached request.transaction_id.
Returns¶
Example¶
Responses¶
Standardized API response wrappers.
Module drf_corekit.http.responses¶
Ensures consistent response structure across all APIs.
wrap_success_response(data=None, status_code=200)¶
Wraps successful API responses in a consistent envelope.
Response format¶
Parameters¶
- data : Any serializable payload
- status_code : HTTP status code (default: 200)
Returns¶
Example¶
FileHttpResponse¶
An HttpResponse subclass for returning file downloads, with the
Content-Disposition header set automatically so the browser saves the file
under a dated name: <file_name>_<YYYYMMDD>.<file_ext>.
| Parameter | Type | Default | Description |
|---|---|---|---|
file |
any |
— | File content, forwarded to HttpResponse as the body |
file_name |
str |
— | Base name used to build the Content-Disposition header |
file_ext |
str |
"pdf" |
Extension appended to the generated file name |
*args |
Forwarded to HttpResponse |
||
**kwargs |
Forwarded to HttpResponse |
def export_invoice(request, invoice_id):
pdf_bytes = render_invoice_pdf(invoice_id)
return FileHttpResponse(pdf_bytes, file_name="invoice", file_ext="pdf")
A request on, say, 3 March 2026 returns a response with:
Tip
The date suffix is derived from timezone.datetime.today() at response time, not from any field on the file itself — two downloads of the same file on different days will get different filenames.