yntnm yntnm - 1 year ago 85
JSON Question

readingJSON with simplejson python

i'm trying to read JSON after i'm sending url request with urllib2.

my code:

request = urllib2.Request("", data=form_data, headers=headers)
response = urllib2.open(request)

So the problem is when i'm trying to read to JSON from the respond object.
i'm doing it like that


the error i get is:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/new/main3/python/simplejson/__init__.py", line 385, in loads
return _default_decoder.decode(s)
File "/opt/new/main3/python/simplejson/decoder.py", line 402, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/opt/new/main3/python/simplejson/decoder.py", line 420, in raw_decode
raise JSONDecodeError("No JSON object could be decoded", s, idx)
simplejson.decoder.JSONDecodeError: No JSON object could be decoded: line 1 column 0 (char 0)

The interesting part is when i'm sending the request in my firefox browser, I'm typing the address at the url line , i do get the json on screen and i can see it at the debug mode as a JSON
{"hosts": [{"host": ""}]}

so the json is valid..

at the debug i'm getting this page:

<!DOCTYPE html>
<!-- framebusting -->
html{display : none ;}
<script type="text/javascript">
if (self == top) {
document.documentElement.style.display = "block";
} else {
top.location = self.location;
<script type="text/javascript" src="js/detectBrowser.js"></script>
<meta charset="utf-8"/>
<link rel="StyleSheet" href="css/forensics.css" type="text/css" media="screen"/>
<script type="text/javascript" src="libs/requirejs/require.js" data-main="js/login.js"></script>

is any one have some way to solve it , or how can i read the json text straight from the respond object , or even look at that at the debug

I appreciate ant help , i'm trying for the least 3 days to figure it out

Answer Source

so it was my bad and the api was need a coockie. after adding a cookie header i was receiving a correct JSON

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download