Library: requests

From Yombo
Jump to: navigation, search
requests
Commonly Used Yes
Status Active

Summary
Requests library simplifies making external HTTP/HTTPS calls.

Introduction

A friendly HTTP helper. Uses the treq python module to process requests. Also provides various helper functions to massage data even if this library wasn't used to make the request.


Hooks

No hooks registered.

Usage

Arguments

Required, unnamed arguments:

  • method - The request method to make, such as GET, POST, PUT.
  • url - The url to access.

Optional named arguments: Defined by the treq.request() function and are passed directly to treq untouched.

  • 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.
  • json (dict, list/tuple, int, string/unicode, bool, or None) – Optional JSON-serializable content to pass in body.
  • 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 or 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.
  • browser_like_redirects (bool) – Use browser like redirects (i.e. Ignore RFC2616 section 10.3 and follow redirects from POST requests). Default: False
  • unbuffered (bool) – Pass True to to disable response buffering. By default treq buffers the entire response body in memory.

Results

Every request returns a dictionary with the following keys:

  • content - Processed content. JSON and MSGPACK items are converted to dictionaries. Otherwise, will be the received content in unicode.
  • content_raw - Raw content as received from the server.
  • content_type - Content type header as received from the server.
  • response - The treq response from the treq.request call.
  • request - The original request that was sent to the server.

Examples

Making Requests

A simple get request:

1 request_results = yield self._Requests.request(
2     'GET',
3     'https://jsonplaceholder.typicode.com/todos/1'
4 )

A post request:

1 request_data = {  # Will be converted to JSON and sent in the request body.
2     "somekey": 123,
3 }
4 request_results = yield self._Requests.request(
5     'POST',
6     'https://jsonplaceholder.typicode.com/todos/1',
7     json=request_data
8 )


This page was last edited on 24 October 2018.