add_header expecting 3 arguments instead of just key/value
python, request-headers, urllib2
Solution
From the documentation, `add_header` takes two arguments. You are calling it with one argument, a tuple with two values.
What you should do:
req.add_header(key, value)
What you are currently doing because you are getting the header as a tuple:
req.add_header((key, value,)) # aka passing a tuple with both arguments to the key parameter
You need to unpack the tuple:
req.add_header(header[0], header[1])
Or even better, using the splat operator (`*`):
req.add_header(*header) # Does the same thing as above
Also, you are using an empty string as the default argument for `header`, when when it is supplied it is a tuple. You should probably change the default value to a tuple or `None`.
Problem
I'm encountering this error message: `TypeError: add_header() takes exactly 3 arguments (2 given)` when using these parameters: `testService("SomeServiceName", "POST", "[redacted valid url]", ('Content-type','application/json'), [redacted valid json])` Normally this error means I'm not passing "self" as a parameter, but seeing as this method is not being called in a class, I'm not sure what to do. I've tried passing self in as a parameter in both the parameters and inside the method. And I've tried wrapping the header in brackets and parentheses. When I pass "self" I get the error message that self is undefined, and when I use the brackets instead of parentheses, I get the same error as above. Anyone with magical Python debugging skills out there? Thanks so much for taking the time to check this out! ``` def testService(name, verb, url, header="", requestBody=""): #Log out the name of the request we're testing if (name is not None) or (name.strip() is not ""): print "Checking " + name + "\n\n" # Make URL with StoreNumber if (url is not None) or (url is not ""): testUrl = url # If specified verb is GET if verb.strip().upper() == "GET": # Create request req = urllib2.Request(testUrl) print "Making request with URL: " + testUrl + "\n\n" # Send request try: response = urllib2.urlopen(req) # If service returns 200 Okay print "Connection to " + name + " Service successful. Returned with code " + str(response.code) + "\n\n" # Log response print "Response: " + response.read() + "\n\n" # Handle exceptions # If HTTP Error except HTTPError as e: if hasattr(e, 'reason'): print name + ' failed to reach a server.' print 'Reason: ', e.reason elif hasattr(e, 'code'): print e.code elif hasattr(e, 'message'): print e.message pass # If URL was the problem except URLError as e: if hasattr(e, 'reason'): print name + ' failed to reach a server.' if str(e.reason) == "[Errno 11004] getaddrinfo failed": print "[Errno 11004] getaddrinfo failed with bad url: " + testUrl + "\n\n" else: print 'Reason: ', e.reason elif hasattr(e, 'code'): print 'Error code: ', e.code elif hasattr(e, 'message'): print e.message pass # If specified verb was POST elif verb.strip().upper() == "POST": # Check for None requestBody if (requestBody is not None) or (requestBody.strip() is not ""): data = urllib.urlencode(requestBody) # Create request req = urllib2.Request(testUrl, data) # Check for header if (header is not None) or (header.strip() is not ""): req.add_header(header) # YO YO THE BELOW CODE IS INCOMPLETE PLEASE FINISH # Log request with URL and Data print "Making request with URL: " + testUrl + " and data: THIS PART IS UNFINISHED PLEASE FINISH ME \n\n" try: response = urllib2.urlopen(req) # If service returns 200 Okay print "Connection to " + name + " Service successful. Returned with code " + str(response.code) + "\n\n" # Log response print "Response: " + response.read() + "\n\n" # Handle exceptions # If HTTP Error except HTTPError as e: if hasattr(e, 'code'): print e.code elif hasattr(e, 'message'): print e.message elif hasattr(e, 'reason'): print name + ' failed to reach a server.' print 'Reason: ', e.reason pass except URLError as e: if hasattr(e, 'reason'): print name + ' failed to reach a server.' if str(e.reason) == "[Errno 11004] getaddrinfo failed": print "[Errno 11004] getaddrinfo failed with bad url: " + url + "\n\n" else: print 'Reason: ', e.reason elif hasattr(e, 'code'): print 'Error code: ', e.code elif hasattr(e, 'message'): print e.message pass # Header non-existent in testService call else: print "Service header not provided. Exiting program" sys.exit() # Requesty Body not present in testService call else: print "Service request body not provided in code. Exiting program" sys.exit() # If specified verb is not supported (Currently only GET and POST are supported) else: print name + " Service written with HTTP verb other than GET or POST. Exiting program" sys.exit() else: print "Service url not provided in code. Exiting program" sys.exit() else: print "Service name not provided in code. Exiting program" sys.exit() ```