I am getting "AttributeError('strip',)" while calling following function to make post request to my endpoint with JSON data
def makePostRequest(apiUrl, body):
try:
jsondata = json.dumps(body)
jsondataasbytes = jsondata.encode('utf-8') # needs to be bytes
len(jsondataasbytes)
req = urllib2.Request(apiUrl, jsondata, {'Content-Type': 'application/json',
'Content-Length': jsondataasbytes})
try:
response = urllib2.Request(req, jsondataasbytes)
response_data = response.read()
print (response_data)
except Exception as err:
print ("Error: %s", err)
except Exception as error:
print ("error in makePostRequest: ", error)
I was doing it the wrong way. It is working fine now after making some changes as follows:
def makePostRequest(apiUrl, body):
try:
req = urllib2.Request(apiUrl)
req.add_header('Content-Type', 'application/json')
jsondata = json.dumps(body)
jsondataasbytes = jsondata.encode('utf-8') # needs to be bytes
req.add_header('Content-Length', len(jsondataasbytes))
try:
response = urllib2.urlopen(req, jsondataasbytes)
response_data = response.read()
print (response_data)
except Exception as err:
print ("Error: %s", err)
except Exception as error:
print ("error in makePostRequest: ", error)