Forums

simplejson.errors.JSONDecodeError

Traceback (most recent call last): File "Jaipur_greater.py", line 26, in <module> data=response.json() File "/usr/lib/python3.8/site-packages/requests/models.py", line 897, in json return complexjson.loads(self.text, **kwargs) File "/usr/lib/python3.8/site-packages/simplejson/init.py", line 518, in loads return _default_decoder.decode(s) File "/usr/lib/python3.8/site-packages/simplejson/decoder.py", line 370, in decode obj, end = self.raw_decode(s) File "/usr/lib/python3.8/site-packages/simplejson/decoder.py", line 400, in raw_decode return self.scan_once(s, idx=_w(s, idx).end()) simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

This is the error when I run my code on the bash console, but it works fine on my device. Can anyone please help?? Here is my code:

import requests
import smtplib
head={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) 
Chrome/39.0.2171.95 Safari/537.36'}
parameter={
        "Accept-Language":"hi_IN",
        "district_id":"505",
        "date" : din
 }
response=requests.get(url="https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByDistrict?"
,params=parameter,headers=head)
print(response.status_code)
data=response.json()
print(data)
length=len(data["sessions"])  
msg=[]
for i in range(length):
     district=data["sessions"][i]["district_name"]
     name=data["sessions"][i]["name"]
     address = data["sessions"][i]["address"]
     age=data["sessions"][i]["min_age_limit"]
     doses = data["sessions"][i]["available_capacity"]
     vaccine=data["sessions"][i]["vaccine"]
     if(age==18 and district=="Jaipur I"):
          info=f"{doses} doses available of {vaccine} for {age}+ at {name},{address}."
          msg.append(info)
 message="\n".join(msg)
 print(message)
 with smtplib.SMTP_SSL("smtp.gmail.com") as connection:
 connection.login(user=mail, password=password)
 connection.sendmail(from_addr=mail,
                    to_addrs=recipient,
                    msg=message.encode("utf-8"))

There's a space in the URL. You're probably not going to the site that you think you are.

No, it is just a typo over here. It runs perfectly on pyCharm, just some problem when i run it here

Then inspect the content of the response you're getting. If it's not JSON, then you need to know what it is.

It is JSON only, I thought the API must not be whitelisted, but it is.

Send us a link to the API documentation and we'll consider it for the whitelist.

Hello there, I am having the exactly same issue with the same site as yours BhavyaGupta42, and here's the API documentation: COWIN-API Documentation And this what I got with response.text (the request.Response object)-

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<TITLE>ERROR: The request could not be satisfied</TITLE>
</HEAD><BODY>
<H1>403 ERROR</H1>
<H2>The request could not be satisfied.</H2>
<HR noshade size="1px">
Request blocked.
We can't connect to the server for this app or website at this time. There might be too much traffic or a configuration error. Try again later, or contact the app or website owner.
<BR clear="all">
If you provide content to customers through CloudFront, you can find steps to troubleshoot and help prevent this error by reviewing the CloudFront documentation.
<BR clear="all">
<HR noshade size="1px">
<PRE>
Generated by cloudfront (CloudFront)
Request ID: AZXRhTRqjymqtmrcmYNi6DKoN9QmPN4u_Mu1ZfC9pKLlbGVf1PCH2Q==
</PRE>
<ADDRESS>
</ADDRESS>
</BODY></HTML>

Please take a look into whitelisting this API. In this Covid-19 pandemic, we really need this as this script will take the vaccine data and mail to people who are in trouble with booking vaccine.

Or if not possible, can you suggest some places where I can upload and run this python script periodically to check and mail the details. Thanks.

cdn-api.co-vin.in is already whitelisted -- we are not blocking the requests, the error is coming from the enpoint you called and it's not PythonAnywhere related. There's nothing we can do about that.

Hello, I found the issue. Cowin servers are currently only accepting requests from India, but PythonAnywhere servers are based in US. Is there any other way such as changing the headers to bypass this.

You can change the request headers -- exactly how to do that depends on which library you use -- but it's likely that the Cowin servers are using the originating IP address of requests to determine the location they're coming from. To work around that you'd need to use a proxy server in India, which unfortunately would not work from a free account.