I am trying to write PyUnit test, where i am passing the URL to the test and the test needs to fetch the data and decode. The data is coming as raw payload. We are using REST. Here is the test code i have written :
#import urllib
import urllib.request
import json
proxy_support = urllib.request.ProxyHandler({"http":"http://local:8080"})
opener = urllib.request.build_opener(proxy_support)
urllib.request.install_opener(opener)
request = urllib.request.Request("http://test.com/1")
response = urllib.request.urlopen(request)
encoding = response.info().get_param('charset', 'utf8')
data = json.loads(response.read().decode(encoding))
print (data)
The problem is that i am getting the following error :
data = json.loads(response.read().decode(encoding))
File "C:\Python34\lib\json__init__.py", line 318, in loads
return _default_decoder.decode(s)
File "C:\Python34\lib\json\decoder.py", line 343, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python34\lib\json\decoder.py", line 361, in raw_decode
raise ValueError(errmsg("Expecting value", s, err.value)) from None
ValueError: Expecting value: line 1 column 1 (char 0)
Could someone help ? Thanks in advance.
Regards
Amit
Related
import requests
r = requests.get("http://link-short.rf.gd/codes/hello/package.json")
r.json()
The codes are showing me
File "c:\file.py", line 3, in <module>
r.json()
File "C:\Users\88019\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\models.py", line 910, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Users\88019\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "C:\Users\88019\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\88019\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
What is the problem and how can I solve that ?
The problem is that JSON is not invalid. To be precise, the problem is not the link, but the file_2.txt file. So you have 2 solution: Check the file_2.text file and get a file that is correctly JSON, because this file is not. Or if you really can't get a JSON file, you can modify your code by inserting a request that applies to your file, because when you run your r (therefore a Requests) it makes assumptions about the encoding of the response based on the headers http. To change the encoding you can replace r.json () with r.text (), so like this:
import requests
r = requests.get ("http://link-short.rf.gd/codes/hello/file_2.txt")
r.text()
UPDATE
I noticed that you later changed the link in the question. Try these
import json
import requests
r = requests.get (http://link-short.rf.gd/codes/hello/package.json)
data = r.json()
or this, because it converts a string to access your JSON
import json
import requests
r = requests.get (http://link-short.rf.gd/codes/hello/package.json)
data = json.loads(r.text)
well http://link-short.rf.gd/codes/hello/file_2.txt does not supply a json.
So you could supply a link to a json file
You cannot call r.json() if r is not a json file. Since your URL points to some file_2.txt, it is not in json() format and hence r.json() will not help you.
Consider using r.text() instead
After generating the access_token (which works when I use it on TD Ameritrade's API website) I'm trying to get option chains for a stock. I can get it to work on TD Ameritrade's API website, and I get an 'OKAY' response when I run my code, but no JSON data attached, any idea why? My relevant code is below.
content = requests.get(url = https://api.tdameritrade.com/v1/marketdata/chains, params = params_dictionary, headers = access_token)
print(content)
print(repr(content.text))
data = content.json()
print(data)
but for my output I get
<Response [200]>
''
Traceback (most recent call last):
File "C:\Users\USER\Documents\GitHub\pythonfiles\TD Ameritrade API tests.py", line 98, in <module>
data = content.json()
File "C:\Users\USER\Anaconda3\lib\site-packages\requests\models.py", line 900, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Users\USER\Anaconda3\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:\Users\USER\Anaconda3\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\USER\Anaconda3\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
JSONDecodeError: Expecting value
It was a mistake on my part unshown in my question. I was accidentally using 'https://api.tdameritrade.com/v1/marketdata/https://api.tdameritrade.com/v1/marketdata/chains' for my URL. I'm unsure how this didn't break things, but this was my issue.
I am getting the error while executing the following python script.
The issue is with converting html output to json . It will be very helpful if you help me to solve the problem.
import urllib2
import json`
url ='http://localhost:40102/cq/etc/replication/agents.author/publish-
u11cmspu1.html'
username='admin'
password='admin'
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, username, password)``
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler)
urllib2.install_opener(opener)
pagehandle = urllib2.urlopen(url)
json_response=json.loads(pagehandle.read())
# output=json.load(pagehandle.read())
print json_response
ERROR :
Traceback (most recent call last):
File "./repQstatus.py", line 18, in <module>
json_response=json.loads(pagehandle.read())
File "/usr/lib64/python2.6/json/__init__.py", line 307, in loads
return _default_decoder.decode(s)
File "/usr/lib64/python2.6/json/decoder.py", line 319, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib64/python2.6/json/decoder.py", line 338, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
What is going on here is the return value of pagehandle.read() is returning an string that is not identifiable as JSON by the python json library.
There could definitely still be JSON inside the response of pagehandle.read(), but something is keeping it from being properly evaluated.
I want to get a json response from server
Here is my code:
import requests
from requests_ntlm import HttpNtlmAuth
r = requests.get('http://helpbook.com/Home/UserInfo?empId=00458974',
auth=HttpNtlmAuth('domain\\login','password'))
print(r.json())
But each time I get the error:
Traceback (most recent call last):
File "C:\Users\user\Desktop\SM\PYTHON_SCRIPTS\Send json query\iso.py", line 6, in <module>
print(r.json())
File "C:\Program Files\Python36\lib\site-packages\requests\models.py", line 894, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Program Files\Python36\lib\json\__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "C:\Program Files\Python36\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Program Files\Python36\lib\json\decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
How to fix it?
I do not have enough reputation to post a comment.
requests cannot decode the JSON response, because there is no JSON response. You should check the return code first:
import requests
from requests_ntlm import HttpNtlmAuth
r = requests.get('http://helpbook.com/Home/UserInfo?empId=00458974',
auth=HttpNtlmAuth('domain\login', 'password'))
if r.status_code == 200:
print(r.json())
else:
print("Request failed: {} - {}".format(r.status_code, r.reason))
And you would get the result:
Request failed: 404 - Not Found
With that information, you can debug you HTTP API and check why you receive 404 error in the first place.
I am trying to get JSON and print VIA python scirpt. I got response from server 200, not sure why i can not able print the file. Please help me on this!!
The Code used is:
Import Requests
response = requests.get("https://Site1/rest/settings/all-server-status", params={'serverId': '56cd7e4d2d0edcace915e674'}, verify=False)
json_data = json.loads(response.text)
I got below error:
Traceback (most recent call last):
File "<pyshell#51>", line 1, in <module>
json_data = json.loads(response.text)
File "C:\Python27\lib\json\__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "C:\Python27\lib\json\decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python27\lib\json\decoder.py", line 384, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
I tried below statements also but no response:
json_data = json.loads(response)
json_data = json.loads(response.read())
The Json Sample output expecting is:
[{"id":"56cd7e4d2d0edcace915e674","protocol":"https","hostName":"x.x.x.x","port":443,"serverName":"Site1","status":"connected","connected":true}]
Thanks in advance!
The POST request you are making is not returning anything, however the Response 200 indicates the connection was successful(No SSL error etc).
The problem is here: params={'serverId': '56cd7e4d2d0edcace915e674'}.
I would suggest debugging your request.get().
Start by checking if the https://hostname.com/key=value is valid.