Search code examples
pythonpython-3.xpython-requestsconfigparser

InvalidSchema("No connection adapters were found for {!r}".format(url)) while using the URL on requests module parsed by ConfigParser


I have an URL in the config file which I parsed using ConfigParser to get the requests

config.ini

[default]
root_url ='https://reqres.in/api/users?page=2'

FetchFeeds.py

import requests
from configparser import ConfigParser
import os
import requests


config = ConfigParser()
config.read(os.path.join(os.path.dirname(__file__), '../Config', 'config.ini'))
rootUrl = (config['default']['root_url'])
print(rootUrl)
response = requests.get(rootUrl)

Even the URL is printed properly 'https://reqres.in/api/users?page=2'but I am getting the following error on the requests module

Traceback (most recent call last):
  File "C:/Users/sam/PycharmProjects/testProject/GET_Request/FetchFeeds.py", line 11, in <module>
    response = requests.get(rootUrl)
  File "C:\Users\sam\PycharmProjects\testProject\venv\lib\site-packages\requests\api.py", line 76, in get
    return request('get', url, params=params, **kwargs)
  File "C:\Users\sam\PycharmProjects\testProject\venv\lib\site-packages\requests\api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Users\sam\PycharmProjects\testProject\venv\lib\site-packages\requests\sessions.py", line 530, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\sam\PycharmProjects\testProject\venv\lib\site-packages\requests\sessions.py", line 637, in send
    adapter = self.get_adapter(url=request.url)
  File "C:\Users\sam\PycharmProjects\testProject\venv\lib\site-packages\requests\sessions.py", line 730, in get_adapter
    raise InvalidSchema("No connection adapters were found for {!r}".format(url))
requests.exceptions.InvalidSchema: No connection adapters were found for "'https://reqres.in/api/users?page=2'"

Process finished with exit code 1

Please note I checked carefully there are no white spaces


Solution

  • The main issue is it tries to find a URL like 'url.com' which doesn't exist (correct would be url.com), so the solution is to not put apostrophes in config.ini files:

    [default]
    root_url = https://reqres.in/api/users?page=2
    

    Also think about if you configure the parameters rather in your code than in the config file and use this instead:

    requests.get(config['default']['root_url'], params={'page': 2})