Making Requests¶
-
treq.
request
(method, url, **kwargs)[source]¶ Make an HTTP request.
Parameters: - method (str) – HTTP method. Example:
'GET'
,'HEAD'
.'PUT'
,'POST'
. - url (str) – http or https URL, which may include query arguments.
- headers (Headers or None) – Optional HTTP Headers to send with this request.
- params (dict w/ str or list/tuple of str values, list of 2-tuples, or None.) – Optional parameters to be append as the query string to the URL, any query string parameters in the URL already will be preserved.
- data (str, file-like, IBodyProducer, or None) – Optional request body.
- reactor – Optional twisted reactor.
- persistent (bool) – Use persistent HTTP connections. Default:
True
- allow_redirects (bool) – Follow HTTP redirects. Default:
True
- auth (tuple of
('username', 'password')
.) – HTTP Basic Authentication information. - cookies (
dict
orcookielib.CookieJar
) – Cookies to send with this request. The HTTP kind, not the tasty kind. - timeout (int) – Request timeout seconds. If a response is not
received within this timeframe, a connection is aborted with
CancelledError
.
Return type: Deferred that fires with an IResponse provider.
- method (str) – HTTP method. Example:
-
treq.
get
(url, headers=None, **kwargs)[source]¶ Make a
GET
request.See
treq.request()
-
treq.
head
(url, **kwargs)[source]¶ Make a
HEAD
request.See
treq.request()
-
treq.
post
(url, data=None, **kwargs)[source]¶ Make a
POST
request.See
treq.request()
-
treq.
put
(url, data=None, **kwargs)[source]¶ Make a
PUT
request.See
treq.request()
-
treq.
patch
(url, data=None, **kwargs)[source]¶ Make a
PATCH
request.See
treq.request()
-
treq.
delete
(url, **kwargs)[source]¶ Make a
DELETE
request.See
treq.request()
Accessing Content¶
-
treq.
collect
(response, collector)[source]¶ Incrementally collect the body of the response.
This function may only be called once for a given response.
Parameters: - response (IResponse) – The HTTP response to collect the body from.
- collector (single argument callable) – A callable to be called each time data is available from the response body.
Return type: Deferred that fires with None when the entire body has been read.
-
treq.
content
(response)[source]¶ Read the contents of an HTTP response.
This function may be called multiple times for a response, it uses a
WeakKeyDictionary
to cache the contents of the response.Parameters: response (IResponse) – The HTTP Response to get the contents of. Return type: Deferred that fires with the content as a str.
-
treq.
text_content
(response, encoding='ISO-8859-1')[source]¶ Read the contents of an HTTP response and decode it with an appropriate charset, which may be guessed from the
Content-Type
header.Parameters: - response (IResponse) – The HTTP Response to get the contents of.
- encoding (str) – An valid charset, such as
UTF-8
orISO-8859-1
.
Return type: Deferred that fires with a unicode.
-
treq.
json_content
(response)[source]¶ Read the contents of an HTTP response and attempt to decode it as JSON.
This function relies on
content()
and so may be called more than once for a given response.Parameters: response (IResponse) – The HTTP Response to get the contents of. Return type: Deferred that fires with the decoded JSON.
Responses¶
-
class
treq.response.
Response
¶ -
collect
(collector)¶ Incrementally collect the body of the response.
Parameters: collector – A single argument callable that will be called with chunks of body data as it is received. Returns: A Deferred that fires when the entire body has been received.
-
content
()¶ Read the entire body all at once.
Returns: A Deferred that fires with a bytes object when the entire body has been received.
-
text
(encoding='ISO-8859-1')¶ Read the entire body all at once as text. :param encoding: An encoding for the body, if none is given the
encoding will be guessed, defaulting to this argument.Returns: A Deferred that fires with a unicode object when the entire body has been received.
-
json
()¶ Read the entire body all at once and decode it as JSON.
Returns: A Deferred that fires with the result of json.loads on the body after it has been received.
-
history
()¶ Get a list of all responses that (such as intermediate redirects), that ultimately ended in the current response.
Returns: A list of treq.response.Response
objects.
Returns: A CookieJar.
Inherited from twisted.web.iweb.IResponse.
-
version
¶
-
code
¶
-
phrase
¶
-
headers
¶
-
length
¶
-
request
¶
-
previousResponse
¶
-
deliverBody
(protocol)¶
-
setPreviousResponse
(response)¶
-