HttpRequest Class

Represents an HTTP request.

URL can be given without query parameters, to be added later using "format_parameters".

Inheritance
builtins.object
HttpRequest

Constructor

HttpRequest(method: str, url: str, headers: Mapping[str, str] | None = None, files: Any | None = None, data: bytes | str | Dict[str, str | int] | None = None)

Parameters

Name Description
method
Required
str

HTTP method (GET, HEAD, etc.)

url
Required
str

At least complete scheme/host/path

headers

HTTP headers

Default value: None
files
dict[str, tuple[str, IO, str, dict]] or dict[str, IO]

Dictionary of 'name': file-like-objects (or {'name': file-tuple}) for multipart encoding upload. file-tuple can be a 2-tuple ('filename', fileobj), 3-tuple ('filename', fileobj, 'content_type') or a 4-tuple ('filename', fileobj, 'content_type', custom_headers), where 'content_type' is a string defining the content type of the given file and custom_headers a dict-like object containing additional headers to add for the file.

Default value: None
data
bytes or dict (<xref:for form>)

Body to be sent.

Default value: None

Methods

format_parameters

Format parameters into a valid query string. It's assumed all parameters have already been quoted as valid URL strings.

prepare_multipart_body

Will prepare the body of this request according to the multipart information.

This call assumes the on_request policies have been applied already in their correct context (sync/async)

Does nothing if "set_multipart_mixed" was never called.

serialize

Serialize this request using application/http spec.

set_bytes_body

Set generic bytes as the body of the request.

Will set content-length.

set_formdata_body

Set form-encoded data as the body of the request.

set_json_body

Set a JSON-friendly object as the body of the request.

set_multipart_mixed

Set the part of a multipart/mixed.

Only supported args for now are HttpRequest objects.

boundary is optional, and one will be generated if you don't provide one. Note that no verification are made on the boundary, this is considered advanced enough so you know how to respect RFC1341 7.2.1 and provide a correct boundary.

Any additional kwargs will be passed into the pipeline context for per-request policy configuration.

set_streamed_data_body

Set a streamable data body.

set_text_body

Set a text as body of the request.

set_xml_body

Set an XML element tree as the body of the request.

format_parameters

Format parameters into a valid query string. It's assumed all parameters have already been quoted as valid URL strings.

format_parameters(params: Dict[str, str]) -> None

Parameters

Name Description
params
Required

A dictionary of parameters.

prepare_multipart_body

Will prepare the body of this request according to the multipart information.

This call assumes the on_request policies have been applied already in their correct context (sync/async)

Does nothing if "set_multipart_mixed" was never called.

prepare_multipart_body(content_index: int = 0) -> int

Parameters

Name Description
content_index
int

The current index of parts within the batch message.

Default value: 0

Returns

Type Description
int

The updated index after all parts in this request have been added.

serialize

Serialize this request using application/http spec.

serialize() -> bytes

Returns

Type Description

The requests serialized as HTTP low-level message in bytes.

set_bytes_body

Set generic bytes as the body of the request.

Will set content-length.

set_bytes_body(data: bytes) -> None

Parameters

Name Description
data
Required

The request field data.

set_formdata_body

Set form-encoded data as the body of the request.

set_formdata_body(data: Dict[str, str] | None = None) -> None

Parameters

Name Description
data

The request field data.

Default value: None

set_json_body

Set a JSON-friendly object as the body of the request.

set_json_body(data: Any) -> None

Parameters

Name Description
data
Required

A JSON serializable object

set_multipart_mixed

Set the part of a multipart/mixed.

Only supported args for now are HttpRequest objects.

boundary is optional, and one will be generated if you don't provide one. Note that no verification are made on the boundary, this is considered advanced enough so you know how to respect RFC1341 7.2.1 and provide a correct boundary.

Any additional kwargs will be passed into the pipeline context for per-request policy configuration.

set_multipart_mixed(*requests: HttpRequest, policies: List[SansIOHTTPPolicy[HTTPRequestType, HTTPResponseType]] | None = None, boundary: str | None = None, **kwargs: Any) -> None

Parameters

Name Description
requests
Required

The requests to add to the multipart/mixed

Keyword-Only Parameters

Name Description
policies

SansIOPolicy to apply at preparation time

boundary
str

Optional boundary

set_streamed_data_body

Set a streamable data body.

set_streamed_data_body(data: Any) -> None

Parameters

Name Description
data
Required
<xref:stream> or <xref:generator> or <xref:asyncgenerator>

The request field data.

set_text_body

Set a text as body of the request.

set_text_body(data: str) -> None

Parameters

Name Description
data
Required
str

A text to send as body.

set_xml_body

Set an XML element tree as the body of the request.

set_xml_body(data: Any) -> None

Parameters

Name Description
data
Required
<xref:<xref:XML node>>

The request field data.

Attributes

body

Alias to data.

Returns

Type Description
str,
dict,

The body of the request.

query

The query parameters of the request as a dict.

Returns

Type Description

The query parameters of the request as a dict.