I have searched in the documentation but couldn’t find any information regarding how to get http headers when using:
result = JSON.ObjectFromURL(data_url, values=values)
Do I need to use HTTP.HTTPRequest and parse the json afterwards?
Hi!
You could do something like this:
json_file = HTTP.Request(data_url, values=values)<br />
headers = json_file.headers<br />
result = JSON.ObjectFromString(json_file.content)<br />
It works for getting the headers (didn't get http response code but that shouldn't matter.)
I have another question, the api I am calling is sometimes returning http code 401 for a page there I need to parse the json in the body.
How should that call be done since I get an error (becouse the http status code is 401 and I don't get any content from the body.
<br />
2012-01-10 21:37:35,955 (-4faed000) : CRITICAL (code:696) - Exception when calling function 'ValidatePrefs' (most recent call last):<br />
File "/Users/<username>/Library/Application Support/Plex Media Server/Plug-ins/Framework.bundle/Contents/Resources/Versions/2/Python/Framework/code.py", line 689, in call_named_function<br />
result = f(*args, **kwargs)<br />
File "/Users/<username>/Library/Application Support/Plex Media Server/Plug-ins/Trakttv.bundle/Contents/Code/__init__.py", line 54, in ValidatePrefs<br />
if talk_to_trakt('account/test', values):<br />
File "/Users/<username>/Library/Application Support/Plex Media Server/Plug-ins/Trakttv.bundle/Contents/Code/__init__.py", line 118, in talk_to_trakt<br />
json_file = HTTP.Request(data_url, values=values, errors='ignore')<br />
File "/Users/<username>/Library/Application Support/Plex Media Server/Plug-ins/Framework.bundle/Contents/Resources/Versions/2/Python/Framework/api/networkkit.py", line 140, in Request<br />
opener=self._opener<br />
File "/Users/<username>/Library/Application Support/Plex Media Server/Plug-ins/Framework.bundle/Contents/Resources/Versions/2/Python/Framework/bases.py", line 169, in _http_request<br />
req = self._core.networking.http_request(url, *args, **kwargs)<br />
File "/Users/<username>/Library/Application Support/Plex Media Server/Plug-ins/Framework.bundle/Contents/Resources/Versions/2/Python/Framework/components/networking.py", line 322, in http_request<br />
return HTTPRequest(self._core, url, data, headers, url_cache, encoding, errors, timeout, immediate, sleep, opener, txn_id)<br />
File "/Users/<username>/Library/Application Support/Plex Media Server/Plug-ins/Framework.bundle/Contents/Resources/Versions/2/Python/Framework/components/networking.py", line 106, in __init__<br />
self.load()<br />
File "/Users/<username>/Library/Application Support/Plex Media Server/Plug-ins/Framework.bundle/Contents/Resources/Versions/2/Python/Framework/components/networking.py", line 119, in load<br />
f = self._opener.open(urllib2.Request(self._url, self._post_data, self._request_headers), timeout=self._timeout)<br />
File "/Users/<username>/Library/Application Support/Plex Media Server/Plug-ins/Framework.bundle/Contents/Resources/Platforms/Shared/Libraries/urllib2_new.py", line 444, in open<br />
response = meth(req, response)<br />
File "/Users/<username>/Library/Application Support/Plex Media Server/Plug-ins/Framework.bundle/Contents/Resources/Platforms/Shared/Libraries/urllib2_new.py", line 557, in http_response<br />
'http', request, response, code, msg, hdrs)<br />
File "/Users/<username>/Library/Application Support/Plex Media Server/Plug-ins/Framework.bundle/Contents/Resources/Platforms/Shared/Libraries/urllib2_new.py", line 482, in error<br />
return self._call_chain(*args)<br />
File "/Users/<username>/Library/Application Support/Plex Media Server/Plug-ins/Framework.bundle/Contents/Resources/Platforms/Shared/Libraries/urllib2_new.py", line 416, in _call_chain<br />
result = func(*args)<br />
File "/Users/<username>/Library/Application Support/Plex Media Server/Plug-ins/Framework.bundle/Contents/Resources/Platforms/Shared/Libraries/urllib2_new.py", line 565, in http_error_default<br />
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)<br />
HTTPError: HTTP Error 401: Unauthorized<br />
<br />
You need to put the code in a try…except block, and catch the HTTPError exception. You can get the status code from that.
Thought HTTPError exception catching didn't work when using HTTP.Request but will try that one. Thanks.
I tried the to catch the HTTPError exception but I am lost, are you sure it is available when using HTTP. class?
My function is like this:
<br />
<br />
try:<br />
json_file = HTTP.Request(data_url, values=values)<br />
headers = json_file.headers<br />
result = JSON.ObjectFromString(json_file.content)<br />
Log(result)<br />
<br />
if result['status'] == 'success':<br />
Log('Trakt responded with: %s' % result['message'])<br />
return True<br />
else:<br />
Log('Trakt responded with: %s' % result['error'])<br />
return False<br />
except HTTP.HTTPError, e:<br />
Log(e.code)<br />
# TODO parse response from read<br />
#result = JSON.ObjectFromString(e.read())<br />
#Log('Trakt responded with: %s' % result['error'])<br />
Log(e.read())<br />
return False<br />
<br />
It works if I use urllib like this: http://docs.python.org/release/3.1.3/howto/urllib2.html#number-1 but I would prefer not to.
Ah, sorry - I should’ve given more detail! The HTTP API uses urllib2 under the hood, so the exceptions are exactly the same. All exceptions exposed by the framework are under Ex, so you need to catch Ex.HTTPError.
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.