Adapter Usage

Creating an Adapter

The standard requests means of using a transport adapter is to mount it on a created session. This is not the only way to load the adapter, however the same interactions will be used.

When mounting an adapter, keep in mind that:

  • for a given URL, requests will use the adapter associated to the longest matching prefix;
  • session are created with adapters for the http:// and https:// prefixes (and thus adapters mounted on http or https will never be used);
  • requests only prepares URLs for http schemes (start with http and only contains letters, numbers, and the + and - signs). In particular params won’t work with the mock:// scheme, but will with http+mock://.

If you are not familiar with adapters, prefer the mocker approach (see Using the Mocker).

>>> import requests
>>> import requests_mock

>>> session = requests.Session()
>>> adapter = requests_mock.Adapter()
>>> session.mount('mock://', adapter)

At this point any requests made by the session to a URI starting with mock:// will be sent to our adapter.