1from __future__
import absolute_import
3from .filepost
import encode_multipart_formdata
6__all__ = [
"RequestMethods"]
11 Convenience mixin for classes who implement a :meth:`urlopen` method, such
12 as :class:`urllib3.HTTPConnectionPool` and
13 :class:`urllib3.PoolManager`.
15 Provides behavior for making common types of HTTP request methods and
16 decides which type of request field encoding to use.
20 :meth:`.request_encode_url` is for sending requests whose fields are
21 encoded in the URL (such as GET, HEAD, DELETE).
23 :meth:`.request_encode_body` is for sending requests whose fields are
24 encoded in the *body* of the request using multipart or www-form-urlencoded
25 (such as for POST, PUT, PATCH).
27 :meth:`.request` is for making any kind of request, it will look up the
28 appropriate encoding format and use one of the above two methods to make
31 Initializer parameters:
34 Headers to include with all requests, unless other headers are given
38 _encode_url_methods = {
"DELETE",
"GET",
"HEAD",
"OPTIONS"}
49 encode_multipart=True,
50 multipart_boundary=None,
54 "Classes extending RequestMethods must implement "
55 "their own ``urlopen`` method."
58 def request(self, method, url, fields=None, headers=None, **urlopen_kw):
60 Make a request using :meth:`urlopen` with the appropriate encoding of
61 ``fields`` based on the ``method`` used.
63 This is a convenience method that requires the least amount of manual
64 effort. It can be used in most situations, while still having the
65 option to drop down to more specific methods when necessary, such as
66 :meth:`request_encode_url`, :meth:`request_encode_body`,
67 or even the lowest level :meth:`urlopen`.
71 urlopen_kw[
"request_url"] = url
75 method, url, fields=fields, headers=headers, **urlopen_kw
79 method, url, fields=fields, headers=headers, **urlopen_kw
84 Make a request using :meth:`urlopen` with the ``fields`` encoded in
85 the url. This is useful for request methods like GET, HEAD, DELETE, etc.
90 extra_kw = {
"headers": headers}
94 url +=
"?" + urlencode(fields)
96 return self.
urlopen(method, url, **extra_kw)
104 encode_multipart=True,
105 multipart_boundary=None,
109 Make a request using :meth:`urlopen` with the ``fields`` encoded in
110 the body. This is useful for request methods like POST, PUT, PATCH, etc.
112 When ``encode_multipart=True`` (default), then
113 :func:`urllib3.encode_multipart_formdata` is used to encode
114 the payload with the appropriate content type. Otherwise
115 :func:`urllib.parse.urlencode` is used with the
116 'application/x-www-form-urlencoded' content type.
118 Multipart encoding must be used when posting files, and it's reasonably
119 safe to use it in other times too. However, it may break request
120 signing, such as with OAuth.
122 Supports an optional ``fields`` parameter of key/value strings AND
123 key/filetuple. A filetuple is a (filename, data, MIME type) tuple where
124 the MIME type is optional. For example::
128 'fakefile': ('foofile.txt', 'contents of foofile'),
129 'realfile': ('barfile.txt', open('realfile').read()),
130 'typedfile': ('bazfile.bin', open('bazfile').read(),
132 'nonamefile': 'contents of nonamefile field',
135 When uploading a file, providing a filename (the first parameter of the
136 tuple) is optional but recommended to best mimic behavior of browsers.
138 Note that if ``headers`` are supplied, the 'Content-Type' header will
139 be overwritten because it depends on the dynamic random boundary string
140 which is used to compose the body of the request. The random boundary
141 string can be explicitly set with the ``multipart_boundary`` parameter.
146 extra_kw = {
"headers": {}}
149 if "body" in urlopen_kw:
151 "request got values for both 'fields' and 'body', can only specify one."
155 body, content_type = encode_multipart_formdata(
156 fields, boundary=multipart_boundary
159 body, content_type = (
161 "application/x-www-form-urlencoded",
164 extra_kw[
"body"] = body
165 extra_kw[
"headers"] = {
"Content-Type": content_type}
167 extra_kw[
"headers"].update(headers)
170 return self.
urlopen(method, url, **extra_kw)
__init__(self, headers=None)
request_encode_body(self, method, url, fields=None, headers=None, encode_multipart=True, multipart_boundary=None, **urlopen_kw)
urlopen(self, method, url, body=None, headers=None, encode_multipart=True, multipart_boundary=None, **kw)
request_encode_url(self, method, url, fields=None, headers=None, **urlopen_kw)