Last updated: Apr 10, 2024
Reading timeยท4 min
requests.get()
method with a string first argumentThe Python requests error "requests.exceptions.InvalidSchema: No connection adapters were found for" occurs for 3 main reasons:
requests.get()
, e.g. passing it a
list, set, dictionary or tuple.http://
or https://
).Here is the complete error message:
File "/home/borislav/anaconda3/lib/python3.9/site-packages/requests/sessions.py", line 792, in get_adapter raise InvalidSchema(f"No connection adapters were found for {url!r}") requests.exceptions.InvalidSchema: No connection adapters were found for "['https://jsonplaceholder.typicode.com/posts']"
Here is an example of how the error occurs.
import requests def make_request(): # ๐๏ธ Incorrectly specifying the URL as a list url = [ 'https://jsonplaceholder.typicode.com/posts' ] res = requests.get(url, timeout=10) parsed = res.json() print(parsed) # โ๏ธ requests.exceptions.InvalidSchema: No connection adapters were found for "['https://jsonplaceholder.typicode.com/posts']" make_request()
The issue in the code sample is that the requests.get()
method expects to get
called with a string and not a list that contains a string.
requests.get()
method with a string first argumentTo solve the error, make sure to call requests.get()
with a string first
argument - the complete URL.
import requests def make_request(): # โ the URL is now a string url = 'https://jsonplaceholder.typicode.com/posts' res = requests.get(url, timeout=10) parsed = res.json() print(parsed) # โ Works make_request()
The URL in the example is now a string that contains a valid URL, so everything works as expected.
If your URL is wrapped in a list, you have to access the list at an index to retrieve the string.
import requests def make_request(): url = ['https://jsonplaceholder.typicode.com/posts'] # ๐๏ธ access first list item res = requests.get(url[0], timeout=10) parsed = res.json() print(parsed) # โ Works make_request()
I've written a detailed article on how to access specific items in a list.
The same approach can be used to access an element in a tuple.
If your URL is stored as a value of a dictionary key, you have to access the
specific key when calling requests.get()
.
import requests def make_request(): url = { 'x': 'https://jsonplaceholder.typicode.com/posts' } # ๐๏ธ access the `x` key in the dictionary res = requests.get(url['x'], timeout=10) parsed = res.json() print(parsed) # โ Works make_request()
We accessed the x
key in the dictionary to retrieve the URL string and passed
the string to the requests.get()
method.
If your URL is stored in a set
, you can
convert the set to a list and access the
first item in the list.
import requests def make_request(): url = {'https://jsonplaceholder.typicode.com/posts'} # ๐๏ธ Convert to list and access first item res = requests.get(list(url)[0], timeout=10) parsed = res.json() print(parsed) # โ Works make_request()
You can use the type() and isinstance() functions to get or check the type of a variable.
url = ['https://jsonplaceholder.typicode.com/posts'] print(type(url)) # ๐๏ธ <class 'list'> print(isinstance(url, list)) # ๐๏ธ True
The error is also caused when you forget to specify the protocol scheme when making a request.
# โ๏ธ Forgot to specify the scheme protocol jsonplaceholder.typicode.com/posts
The URL above is incomplete because the protocol is not specified (e.g.
http://
or https://
).
Make sure to specify the protocol when making a request.
# โ Correct (protocol is specified) http://jsonplaceholder.typicode.com/posts https://jsonplaceholder.typicode.com/posts
The protocol could either be http://
or https://
depending on whether the
connection to the origin is secure.
If you omit the protocol, the requests
library doesn't know how to connect to
the specified origin.
The protocol should be in all lowercase letters as https://
is not equal to
HTTPS://
.
Make sure that your URL doesn't contain any special characters (e.g. newlines or quotes).
url = ''' https://jsonplaceholder.typicode.com/posts ''' # '\n https://jsonplaceholder.typicode.com/posts\n ' print(repr(url))
The example uses a triple-quoted string to specify the URL.
However, there are newline characters at the start and end of the URL which makes it invalid.
Make sure you don't have any quotes in the URL string either.
Another common cause of the error is defining a tuple by mistake when specifying the URL.
url = 'https://jsonplaceholder.typicode.com/posts', # ๐๏ธ ('https://jsonplaceholder.typicode.com/posts',) print(url) print(type(url)) # ๐๏ธ <class 'tuple'>
Notice that there is a trailing comma at the end of the URL string.
Tuples are constructed in multiple ways:
()
creates an empty tuplea,
or (a,)
a, b
or (a, b)
tuple()
constructorIn other words, when there is a trailing comma after a string, you define a tuple containing a string.
Instead, remove the trailing comma to define a string.
url = 'https://jsonplaceholder.typicode.com/posts', # ๐๏ธ 'https://jsonplaceholder.typicode.com/posts' print(url)
Or access the tuple at index 0
to get its first element.
url = 'https://jsonplaceholder.typicode.com/posts', # ๐๏ธ 'https://jsonplaceholder.typicode.com/posts' print(url[0]) print(type(url[0])) # ๐๏ธ <class 'str'>
You can use the type() and isinstance() functions to get or check the type of a variable.
url = ['https://jsonplaceholder.typicode.com/posts'] print(type(url)) # ๐๏ธ <class 'list'> print(isinstance(url, list)) # ๐๏ธ True
To solve the "requests.exceptions.InvalidSchema: No connection adapters were found for" error in Python, make sure:
requests.get()
is a valid URL string.http://
or https://
).You can learn more about the related topics by checking out the following tutorials: