Home > database >  Python Requests: why do I get the error "get() takes 2 positional arguments but 3 were given&qu
Python Requests: why do I get the error "get() takes 2 positional arguments but 3 were given&qu

Time:02-04

I'm trying to implement error-handling of 504 responses for my Requests statement when I query an API. I read through this question, and am attempting to implement the solution given in the most-voted answer.

My code keeps failing with the error get() takes 2 positional arguments but 3 were given. In my code (below), I'm only passing get() two arguments, though. Where am I going wrong?

Here's the full error TraceBack:

Exception has occurred: TypeError
get() takes 2 positional arguments but 3 were given
  File " ... ", line 113, in my_function
    s.get("https://epqs.nationalmap.gov/v1/json", payload)
  File " ... ",  line 158, in <module>
    d = my_function(l)
TypeError: get() takes 2 positional arguments but 3 were given

My code:

payload = {
   "x": coordinate[0],
   "y": coordinate[1],
   "wkid": 4326,
   "units": "Meter",
   "includeDate": "false",
}
s = requests.Session()
retries = Retry(
   total=10,
   backoff_factor=1,
   status_forcelist=[502, 503, 504],
)
s.mount("https://", HTTPAdapter(max_retries=retries))
s.get("https://epqs.nationalmap.gov/v1/json", payload)

My previous code that didn't make use of Session() or mount() worked, so does Session() add some data behind the scenes to the get() request that I'm overlooking?

Here's my previous code, fwiw:

payload = {
   "x": coordinate[0],
   "y": coordinate[1],
   "wkid": 4326,
   "units": "Meter",
   "includeDate": "false",
}
r = requests.get("https://epqs.nationalmap.gov/v1/json", payload)

CodePudding user response:

Only (as far as I can tell) requests.get allows you to pass the parameters as a positional argument. You must use a keyword argument for Session.get.

s.get("https://epqs.nationalmap.gov/v1/json", params=payload)

(The two allowed positional arguments are the Session object s implicit to the method call and the URL; the payload is the 3rd.)

CodePudding user response:

s.get is an instance method, so the first positional argument is in fact self which is implicitly passed when the method is called. See below:

class foo:
    def bar(self, a, b):
        return a b
        
foo().bar(1,2,3)
# TypeError: ’bar’ takes three
# arguments, but four were passed:
#    self, 1, 2, and 3
  • Related