Skip to content

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

(site_name: str, site_domain: str, protocol: str)

Behavior

  • Protocol is:
  • http if DEBUG=True
  • https otherwise

Example

name, domain, protocol = get_current_site_info(request)

get_client_ip(request)

Extracts the client IP address, respecting common proxy headers.

Priority

  1. HTTP_CF_CONNECTING_IP
  2. HTTP_TRUE_CLIENT_IP
  3. HTTP_X_FORWARDED_FOR (first valid IP)
  4. HTTP_X_REAL_IP
  5. HTTP_FORWARDED (for= token)
  6. REMOTE_ADDR

Returns

str | None

Example

ip = get_client_ip(request)

get_request_txid(request)

Retrieves request transaction ID if available.

This assumes a middleware has attached request.transaction_id.

Returns

str | None

Example

txid = get_request_txid(request)

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

{
  "data": "...",
  "errors": null
}

Parameters

  • data : Any serializable payload
  • status_code : HTTP status code (default: 200)

Returns

rest_framework.response.Response

Example

return wrap_success_response({"id": 1, "name": "Abu"})

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:

Content-Disposition: attachment; filename=invoice_20260303.pdf
Content-Transfer-Encoding: binary

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.