Python Requests: No connection adapters were found for

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
4 min

banner

# Table of Contents

  1. Python Requests: No connection adapters were found for
  2. Make sure to call the requests.get() method with a string first argument
  3. Make sure to specify the protocol scheme when making a request
  4. Make sure your URL doesn't contain special characters (e.g. newlines)
  5. Defining a tuple by mistake

# Python Requests: No connection adapters were found for

The Python requests error "requests.exceptions.InvalidSchema: No connection adapters were found for" occurs for 3 main reasons:

  1. Passing a value that is not a string to requests.get(), e.g. passing it a list, set, dictionary or tuple.
  2. Forgetting to specify the protocol scheme in the URL string (e.g. http:// or https://).
  3. Your URL contains invalid characters, e.g. newlines.

invalid schema no connection adapters were found for

Here is the complete error message:

shell
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.

main.py
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 code for this article is available on GitHub

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.

# Make sure to call the requests.get() method with a string first argument

To solve the error, make sure to call requests.get() with a string first argument - the complete URL.

main.py
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 code for this article is available on GitHub

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.

main.py
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().

main.py
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()
The code for this article is available on GitHub

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.

main.py
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.

main.py
url = ['https://jsonplaceholder.typicode.com/posts'] print(type(url)) # ๐Ÿ‘‰๏ธ <class 'list'> print(isinstance(url, list)) # ๐Ÿ‘‰๏ธ True
The code for this article is available on GitHub

# Make sure to specify the protocol scheme when making a request

The error is also caused when you forget to specify the protocol scheme when making a request.

shell
# โ›”๏ธ 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.

shell
# โœ… 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 your URL doesn't contain special characters (e.g. newlines)

Make sure that your URL doesn't contain any special characters (e.g. newlines or quotes).

main.py
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.

# Defining a tuple by mistake

Another common cause of the error is defining a tuple by mistake when specifying the URL.

main.py
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:

  • Using a pair of parentheses () creates an empty tuple
  • Using a trailing comma - a, or (a,)
  • Separating items with commas - a, b or (a, b)
  • Using the tuple() constructor

In 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.

main.py
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.

main.py
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.

main.py
url = ['https://jsonplaceholder.typicode.com/posts'] print(type(url)) # ๐Ÿ‘‰๏ธ <class 'list'> print(isinstance(url, list)) # ๐Ÿ‘‰๏ธ True
The code for this article is available on GitHub

# Conclusion

To solve the "requests.exceptions.InvalidSchema: No connection adapters were found for" error in Python, make sure:

  1. The value you are passing to requests.get() is a valid URL string.
  2. You haven't forgotten to specify the protocol scheme in the URL string (e.g. http:// or https://).
  3. Your URL doesn't contain invalid characters, e.g. newlines or quotes.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev