Request Fields

These classes encapsulate a request parameters or fields and are suitable for passing to request().

class urllib3.fields.RequestField(name: str, data: str | bytes, filename: str | None = None, headers: Mapping[str, str] | None = None, header_formatter: Callable[[str, str | bytes], str] | None = None)[source]

A data container for request body parameters.

Parameters:
  • name – The name of this request field. Must be unicode.

  • data – The data/value body.

  • filename – An optional filename of the request field. Must be unicode.

  • headers – An optional dict-like object of headers to initially use for the field.

Changed in version 2.0.0: The header_formatter parameter is deprecated and will be removed in urllib3 v2.1.0.

_render_part(name: str, value: str | bytes) str[source]

Override this method to change how each multipart header parameter is formatted. By default, this calls format_multipart_header_param().

Parameters:
  • name – The name of the parameter, an ASCII-only str.

  • value – The value of the parameter, a str or UTF-8 encoded bytes.

classmethod from_tuples(fieldname: str, value: str | bytes | Tuple[str, str | bytes] | Tuple[str, str | bytes, str], header_formatter: Callable[[str, str | bytes], str] | None = None) RequestField[source]

A RequestField factory from old-style tuple parameters.

Supports constructing RequestField from parameter of key/value strings AND key/filetuple. A filetuple is a (filename, data, MIME type) tuple where the MIME type is optional. For example:

'foo': 'bar',
'fakefile': ('foofile.txt', 'contents of foofile'),
'realfile': ('barfile.txt', open('realfile').read()),
'typedfile': ('bazfile.bin', open('bazfile').read(), 'image/jpeg'),
'nonamefile': 'contents of nonamefile field',

Field names and filenames must be unicode.

make_multipart(content_disposition: str | None = None, content_type: str | None = None, content_location: str | None = None) None[source]

Makes this request field into a multipart request field.

This method overrides “Content-Disposition”, “Content-Type” and “Content-Location” headers to the request parameter.

Parameters:
  • content_disposition – The ‘Content-Disposition’ of the request body. Defaults to ‘form-data’

  • content_type – The ‘Content-Type’ of the request body.

  • content_location – The ‘Content-Location’ of the request body.

render_headers() str[source]

Renders the headers for this request field.