duck.http.response_payload

Module representing response payload classes.

Module Contents

Classes

BaseResponsePayload

BaseResponsePayload class.

HttpResponsePayload

ResponsePayload class for storing response top header and headers.

SimpleHttpResponsePayload

A class representing a simplified HTTP response payload, storing basic response metadata such as the top header (status line) and HTTP headers.

API

class duck.http.response_payload.BaseResponsePayload[source]

BaseResponsePayload class.

property cookies: http.cookies.SimpleCookie

Property getter for cookies. If not already initialized, it initializes the cookies.

Returns:

The cookies for the response.

Return type:

SimpleCookie

Delete a cookie from the response payload by setting its expiration date to the past. This will prompt the client to remove the cookie.

Parameters:
  • key – The name of the cookie to delete.

  • path – The path for which the cookie was set. Defaults to “/”.

  • domain – The domain for which the cookie was set. Defaults to None.

get_all_cookies() Dict[str, str][source]

Retrieves all cookies as a dictionary.

Returns:

A dictionary of all cookies, where the key is the cookie name and the value is the cookie value (without other cookie properties).

Return type:

Dict[str, str]

Retrieves the value of a specific cookie by name.

Parameters:

name – The name of the cookie to retrieve.

Returns:

The cookie value, or an empty string if the cookie does not exist.

Return type:

str

Retrieves the cookie object/morsel of a specific cookie by name.

Parameters:

name – The name of the cookie to retrieve.

Returns:

The cookie object, or None if the cookie does not exist.

Return type:

Optional[Morsel]

Returns the cookie string for the provided name with all fields including max-age, domain, path, etc.

Parameters:

include_cookie_name – Whether to cookie name e.g. cookie=something;. Defaults to True.

abstract property raw

Set a custom cookie on the response payload.

Parameters:
  • key – The name of the cookie (e.g., ‘csrftoken’).

  • value – The value of the cookie (e.g., ‘some_random_token’).

  • domain – The domain to set for the cookie. Defaults to None.

  • path – The path for the cookie. Defaults to ‘/’ indicating the whole site.

  • max_age – The maximum age of the cookie in seconds or as a timedelta object.

  • expires – The expiration date of the cookie. Defaults to None.

  • secure – Whether the cookie should only be sent over HTTPS connections. Defaults to False.

  • httponly – Whether the cookie should be inaccessible to JavaScript. Defaults to False.

  • samesite – The SameSite attribute for the cookie. Default is ‘Lax’. Other possible values are ‘Strict’ or ‘None’.

Raises:

ValueError – If an invalid value is provided for samesite.

set_multiple_cookies(cookies: Dict[str, Dict[str, Any]]) None[source]

Sets multiple cookies at once. Each cookie is specified by a dictionary of attributes.

Parameters:

cookies – A dictionary where the key is the cookie name and the value is another dictionary of cookie attributes.

class duck.http.response_payload.HttpResponsePayload(**kwargs)[source]

Bases: duck.http.response_payload.BaseResponsePayload

ResponsePayload class for storing response top header and headers.

Example payload:

HTTP/1.1 200 OK

Connection: close

Content-Type: text/html

Initialization

Parameters:
  • status_code – The status code for the response. Default value is 200.

  • status_message – The status message corresponding to the status code.

  • headers – The headers to attach to the response payload.

  • http_version – The HTTP version for the server. Default value is ‘HTTP/2.1’

  • explanation – A longer explanation of the status message.

__repr__()[source]
get_header(header: str, default_value=None) Optional[str][source]

Returns a header value of default if not found.

parse_status(code: int, msg: str = None, explanation: str = None)[source]

Parses topheader status for the payload.

property raw: bytes

Returns the raw bytes representing the payload (header section).

set_header(header: str, value: str)[source]

Sets header and value for the payload

class duck.http.response_payload.SimpleHttpResponsePayload(topheader: str = '', headers: Optional[Dict] = None)[source]

Bases: duck.http.response_payload.BaseResponsePayload

A class representing a simplified HTTP response payload, storing basic response metadata such as the top header (status line) and HTTP headers.

This class is used for lightweight storage and manipulation of HTTP response headers and status-related information. It does not handle the response body.

Variables:
  • cookies – The http.cookies.SimpleCookie object for all cookies.

  • topheader – The status line of the HTTP response (e.g., “200 OK”). Defaults to an empty string.

  • headers – A Headers object containing the HTTP response headers. Defaults to None.

Methods:

  • set_topheader(topheader: str): Updates the top header (status line) of the response.

  • set_headers(headers: dict): Updates the HTTP headers using a dictionary input.

Properties:

  • status_code (int): Extracts and returns the status code from the topheader.

  • status_message (str): Extracts and returns the status message from the topheader. Defaults to an empty string if the topheader is not set.

  • explanation (str): A placeholder for additional explanation or description of the status. Currently always returns an empty string.

Initialization

Initializes a SimpleHttpResponsePayload instance.

Parameters:
  • topheader – The HTTP response status line (e.g., “HTTP/1.1 200 OK”). Defaults to an empty string.

  • headers – A dictionary representing the HTTP response headers. Defaults to None.

__repr__()[source]
property explanation: str

Placeholder for additional status explanation.

Returns:

Always returns an empty string.

Return type:

str

get_header(header: str, default_value=None) Optional[str][source]

Returns a header value of default if not found.

property http_version: str

Extracts and returns the HTTP version from the topheader.

This property analyzes the topheader (status line) of the HTTP response and retrieves the HTTP version, such as “HTTP/1.1” or “HTTP/2”. If the topheader is not set or improperly formatted, it defaults to the last version in HttpRequest.SUPPORTED_HTTP_VERSIONS.

Returns:

The HTTP version extracted from the topheader or the default version if unavailable.

Return type:

str

property raw: bytes
    Constructs and returns the raw HTTP response as bytes.

    This property generates a raw representation of the HTTP response by combining the `topheader` (status line)
    and all headers stored in the `headers` attribute. Each header is formatted as "Header-Name: value", separated
    by carriage return and newline characters (`

`). The resulting raw response is encoded as UTF-8.

    Returns:
        bytes: The raw HTTP response as bytes.
set_header(header: str, value: str)[source]

Sets header and value for the payload

set_topheader(topheader: str)[source]

Updates the status line of the HTTP response.

Parameters:

topheader – The new HTTP status line (e.g., “404 Not Found”).

property status_code: int

Extracts and returns the HTTP status code from the status line.

Returns:

The HTTP status code (e.g., 200).

Return type:

int

property status_message: str

Extracts and returns the HTTP status message from the status line.

Returns:

The HTTP status message (e.g., “OK”).

Return type:

str