I am working on python code for some webhook urls. Earlier the code was working on python 2.7 and had to be updated to python 3 Python 2 to 3 conversion was done using 2to3. Python 2 code :
def send_webhook_request(url, body, user_agent=None): if url is None: print >> sys.stderr, "ERROR No URL provided" return False print >> sys.stderr, "INFO Sending POST request to url=%s with size=%d bytes payload" % (url, len(body)) print >> sys.stderr, "DEBUG Body: %s" % body try: user="USER" password="PASSWORD" credentials = (user + ':' + password).encode('utf-8') base64_encoded_credentials = base64.b64encode(credentials).decode('utf-8') headers = {'Authorization': 'Basic ' + base64_encoded_credentials, "Content-Type": "application/json", 'User-Agent': user_agent} #req = urllib.urlopen(url, body, headers) req = urllib2.Request(url, body, headers) res = urllib2.urlopen(req) response = res.read() #res = urllib2.read() if 200 <= res.code < 300: print >> sys.stderr, "INFO Webhook receiver responded with HTTP status=%d" % res.code return response else: print >> sys.stderr, "ERROR Webhook receiver responded with HTTP status=%d" % res.code return False except urllib2.HTTPError, e: print >> sys.stderr, "ERROR Error sending webhook request: %s" % e except urllib2.URLError, e: print >> sys.stderr, "ERROR Error sending webhook request: %s" % e except ValueError, e: print >> sys.stderr, "ERROR Invalid URL: %s" % e return False Python 3 Code : EDIT 1 - added encode to body.
def send_webhook_request(url, body, user_agent=None): if url is None: print("ERROR No URL provided", file=sys.stderr) return False print("INFO Sending POST request to url=%s with size=%d bytes payload" % (url, len(body)), file=sys.stderr) print("DEBUG Body: %s" % body, file=sys.stderr) try: user="integration.argossplunk" password="hv6ep_gXR+M$#8tk@e4cePYx@*Er4VD#" credentials = (user + ':' + password).encode('utf-8') base64_encoded_credentials = base64.b64encode(credentials).decode('utf-8') headers = {'Authorization': 'Basic ' + base64_encoded_credentials, "Content-Type": "application/json", 'User-Agent': user_agent} #req = urllib.urlopen(url, body, headers) body = body.encode() #body = urllib.parse.urlencode(body).encode("utf-8") req = urllib.request.Request(url, body, headers) res = urllib.request.urlopen(req) response = res.read() #res = urllib2.read() if 200 <= res.code < 300: print("INFO Webhook receiver responded with HTTP status=%d" % res.code, file=sys.stderr) return response else: print("ERROR Webhook receiver responded with HTTP status=%d" % res.code, file=sys.stderr) return False except urllib.error.HTTPError as e: print("ERROR Error sending webhook request: %s" % e, file=sys.stderr) except urllib.error.URLError as e: print("ERROR Error sending webhook request: %s" % e, file=sys.stderr) except ValueError as e: print("ERROR Invalid URL: %s" % e, file=sys.stderr) return False When I am calling this fuction :
send_webhook_request(url, json.dumps(body), user_agent=user_agent) It gives me error - TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.
Please suggest what can be done ?
Thanks
urllibto usingrequests, but for your code, if you read the error message and consult the documentation forurllib.request.Requestdocs.python.org/3/library/… you'll see that the data parameter has to be bytes - but as the error message says you're providing a value which is astr. You can convert (encode) str to bytes usingencode()see docs.python.org/3/library/…