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://
andhttps://
prefixes (and thus adapters mounted onhttp
orhttps
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 themock://
scheme, but will withhttp+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.