c12 c12 - 1 month ago 23
Python Question

404 being returned by urllib2.urlopen(req) even though HTTP 200 being sent

I have the below python script that is making a successful rest web service request but then throwing a 404 after the server sends a HTTP 200 back. I can see the rest web service web app log returning the below JSON response successfully. Any ideas on how to further troubleshoot this or what could be causing the below error?

Last Python line executed:

result = urllib2.urlopen(req)


JSON response from Rest API Endpoint:

response{"status":"SUCCESS","reason":null,"location":null,"details":null,"errorDetails":{}}


Python Script:

#!/home/user/activepython-2.7.2.5_x86_64/bin/python

import sys
import os
import logging
import subprocess
import tempfile
import json
import urllib2

# define SVN
svn_repo = sys.argv[1]
svn_txn = sys.argv[2]
svn_opt = '-t'

# handle temp file
tmp_file = tempfile.NamedTemporaryFile(prefix="app_",
suffix=".tmp", dir="/tmp", delete=False)
delete_file = True

rest_url = 'https://host/rest/api/endpoint'

# setup logging
log_level = logging.DEBUG
logger = logging.getLogger("myapp")
logger.setLevel(log_level)
handler = logging.StreamHandler(sys.stderr)
handler.setLevel(log_level)
logger.addHandler(handler)

def get_svn_changes():
cmd = "/home/user/bin/svnlook changed --copy-info %s %s %s" % (svn_opt, svn_txn, svn_repo)
output, return_code = command_output(cmd)
return output

def get_author():
cmd = "/home/csvn/bin/svnlook author %s %s %s" % (svn_opt, svn_txn, svn_repo)
author, return_code = command_output(cmd)
return author.strip()

def call_webservice():
req = urllib2.Request(rest_url)
req.add_header('Accept', 'arpplication/json')
req.add_header('Content-Type', 'application/json')
logger.debug("file=" + tmp_file.name)
data = json.dumps({"name" : "file", "value" : tmp_file.name})
logger.debug("data=" + data)
req.add_data(data)

logger.debug("request")
result = urllib2.urlopen(req)
logger.debug("result")
json_result = json.load(result)
logger.debug("json_result")
result_data = json.loads(json_result['response'])
logger.debug("result_data")
return result_data

if __name__ == "__main__":

exit_code = 0;
out_message = ''
author = get_author()

try:
tmp_file.write("author=%s\n" % author)
output = get_svn_changes()
tmp_file.write(output)
tmp_file.close()
output = call_webservice()

if (output['status'] == 'ERROR'):
out_message = output['reason']
exit_code = 1

except Exception, ex:
out_message = str(ex)
exit_code = 1

finally:
if (exit_code == 1):
sys.stderr.write("Error: %s" % out_message)
if delete_file:
os.remove(tmp_file.name)
sys.exit(exit_code)


Traceback Exception:

//startlogger output
file=/tmp/app_rrOgN0.tmp
data={"name": "file", "value": "/tmp/app_rrOgN0.tmp"}
request
//stop logger output
Traceback (most recent call last):
File "/home/csvn/data/repositories/repo/hooks/pre-commit", line 85, in <module>
output = call_webservice()
File "/home/csvn/data/repositories/repo/hooks/pre-commit", line 59, in call_webservice
result = urllib2.urlopen(req)
File "/home/activepython-2.7.2.5_x86_64/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/home/activepython-2.7.2.5_x86_64/lib/python2.7/urllib2.py", line 400, in open
response = meth(req, response)
File "/home/activepython-2.7.2.5_x86_64/lib/python2.7/urllib2.py", line 513, in http_response
'http', request, response, code, msg, hdrs)
File "/home/activepython-2.7.2.5_x86_64/lib/python2.7/urllib2.py", line 438, in error
return self._call_chain(*args)
File "/home/activepython-2.7.2.5_x86_64/lib/python2.7/urllib2.py", line 372, in _call_chain
result = func(*args)
File "/home/activepython-2.7.2.5_x86_64/lib/python2.7/urllib2.py", line 521, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 404: Not Found
Error: HTTP Error 404: Not Found

Answer

You misspelled the Accept header:

req.add_header('Accept', 'arpplication/json')

Correct the spelling of application:

req.add_header('Accept', 'application/json')
Comments