Here is (from a high-level point of view) the mechanism behind requests:
When the request is constructed request(method, url, **kwargs)
only the method and url arguments are mandatory the rest are optional:
:param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.
Afterwards from the methods perspective:
def get(url, params=None, **kwargs):
r"""Sends a GET request.
:param url: URL for the new :class:`Request` object.
:param params: (optional) Dictionary, list of tuples or bytes to send
in the query string for the :class:`Request`.
:param \*\*kwargs: Optional arguments that ``request`` takes.
:return: :class:`Response <Response>` object
:rtype: requests.Response
"""
kwargs.setdefault('allow_redirects', True)
return request('get', url, params=params, **kwargs)
def post(url, data=None, json=None, **kwargs):
r"""Sends a POST request.
:param url: URL for the new :class:`Request` object.
:param data: (optional) Dictionary, list of tuples, bytes, or file-like
object to send in the body of the :class:`Request`.
:param json: (optional) json data to send in the body of the :class:`Request`.
:param \*\*kwargs: Optional arguments that ``request`` takes.
:return: :class:`Response <Response>` object
:rtype: requests.Response
"""
return request('post', url, data=data, json=json, **kwargs)
For get
and post
mandatory argument is the url and the others are default or optional.
Many web services may require authentication like HTTP Basic Auth. This is the simplest kind, and Requests supports it straight out of the box.
from requests.auth import HTTPBasicAuth
requests.get('http://host:port/api/users', auth=('user', 'pass'))
Which is the same with
from requests.auth import HTTPBasicAuth
requests.get('http://host:port/api/user', auth=HTTPBasicAuth('user', 'pass'))
So basically it's very important how the API was implemented from the authentication point of view (HTTPBasicAuth, HTTPDigest Authentication, OAuth1). Based on this you can use the appropriate module(within requests
) in order to authenticate.
Hope this helps