I come across 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])
Usually this error means that I do not pass the “I” as a parameter, but, seeing that this method is not being called in the class, I'm not sure what to do. I tried to pass myself as a parameter both in the parameters and inside the method. And I tried to wrap the title in brackets and parentheses. When I pass "I", I get the error message that self is undefined, and when I use parentheses instead of parentheses, I get the same error as above.
Anyone with magical Python debugging skills? Thanks so much for taking the time to check this out!
def testService(name, verb, url, header="", requestBody=""):
if (name is not None) or (name.strip() is not ""):
print "Checking " + name + "\n\n"
if (url is not None) or (url is not ""):
testUrl = url
if verb.strip().upper() == "GET":
req = urllib2.Request(testUrl)
print "Making request with URL: " + testUrl + "\n\n"
try:
response = urllib2.urlopen(req)
print "Connection to " + name + " Service successful. Returned with code " + str(response.code) + "\n\n"
print "Response: " + response.read() + "\n\n"
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
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
elif verb.strip().upper() == "POST":
if (requestBody is not None) or (requestBody.strip() is not ""):
data = urllib.urlencode(requestBody)
req = urllib2.Request(testUrl, data)
if (header is not None) or (header.strip() is not ""):
req.add_header(header)
print "Making request with URL: " + testUrl + " and data: THIS PART IS UNFINISHED PLEASE FINISH ME \n\n"
try:
response = urllib2.urlopen(req)
print "Connection to " + name + " Service successful. Returned with code " + str(response.code) + "\n\n"
print "Response: " + response.read() + "\n\n"
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
else:
print "Service header not provided. Exiting program"
sys.exit()
else:
print "Service request body not provided in code. Exiting program"
sys.exit()
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()