Using the requests module
Another option to establish client-server communication among microservices is the requests
module. Although httpx
and requests
are almost compatible, the latter offers other features such as auto-redirection and explicit session handling. The only problem with requests
is its non-direct support to asynchronous APIs and its slow performance when accessing resources. Despite its drawbacks, the requests
module is still the standard way of consuming REST APIs in Python microservice development. First, we need to install it before we can use it:
pip install requests
In our ERP prototype, the requests
extension was used by the faculty microservice to borrow books from the library module. Let’s look at the Faculty client services that show us how the requests
module is used to access the synchronous API of library:
@router.get('/books/request/list') def list_all_request(): with requests.Session() as sess: ...