Azd325
Azd325

Reputation: 6130

Python Requests - No connection adapters

I'm using the Requests: HTTP for Humans library and I got this weird error and I don't know what is mean.

No connection adapters were found for '192.168.1.61:8080/api/call'

Anybody has an idea?

Upvotes: 397

Views: 423152

Answers (7)

Visionscaper
Visionscaper

Reputation: 4119

In my case the issue was that I had defined URLs as a value to an environment variable in an .env, like this:

MY_ENDPOINT="<url>"

The problem with this is that, at least Docker, takes the quotes as part of the URL string, making the URL invalid. Removing the quotes solved the issue for me:

MY_ENDPOINT=<url>

Upvotes: 1

Susobhan Das
Susobhan Das

Reputation: 1144

I was trying to call json-server with requests, however I was unable to call the API.

The root problem was, it requires to be included the protocol scheme i.e. http://

import requests

url = 'http://localhost:3000/employees'

try:
  response = requests.get(url)
  if response:
    print(response.json())
except requests.exceptions.RequestException as ex:
  print(ex)

Also, for http requests - the requests should be wrapped with try/except block - to better debug the cause of the problem.

Upvotes: 0

Vitalii Mytenko
Vitalii Mytenko

Reputation: 772

In my case I used default url for method

def call_url(url: str = "https://www.google.com"):

and url attr was overridden by some method to /some/url

Upvotes: 0

Gulzar
Gulzar

Reputation: 27946

As stated in a comment by christian-long

Your url may accidentally be a tuple because of a trailing comma

url = self.base_url % endpoint,

Make sure it is a string

Upvotes: 2

Yaakov Bressler
Yaakov Bressler

Reputation: 12018

In my case, I received this error when I refactored a url, leaving an erroneous comma thus converting my url from a string into a tuple.

My exact error message:

    741         # Nothing matches :-/
--> 742         raise InvalidSchema("No connection adapters were found for {!r}".format(url))
    743 
    744     def close(self):

InvalidSchema: No connection adapters were found for "('https://api.foo.com/data',)"

Here's how that error came to be born:

# Original code:
response = requests.get("api.%s.com/data" % "foo", headers=headers)

# --------------
# Modified code (with bug!)
api_name = "foo"
url = f"api.{api_name}.com/data",  # !!! Extra comma doesn't belong here!
response = requests.get(url, headers=headers)


# --------------
# Solution: Remove erroneous comma!
api_name = "foo"
url = f"api.{api_name}.com/data"  # No extra comma!
response = requests.get(url, headers=headers)

Upvotes: 9

Martijn Pieters
Martijn Pieters

Reputation: 1121864

You need to include the protocol scheme:

'http://192.168.1.61:8080/api/call'

Without the http:// part, requests has no idea how to connect to the remote server.

Note that the protocol scheme must be all lowercase; if your URL starts with HTTP:// for example, it won’t find the http:// connection adapter either.

Upvotes: 697

Kingname
Kingname

Reputation: 1362

One more reason, maybe your url include some hiden characters, such as '\n'.

If you define your url like below, this exception will raise:

url = '''
http://google.com
'''

because there are '\n' hide in the string. The url in fact become:

\nhttp://google.com\n

Upvotes: 53

Related Questions