I am fairly new to API world and Python. I am trying to make asynchronous call to an API which ran fine in VSCode IDE but gives me issues when I try to run on a Linux environment. I keep getting an “json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)” error. Python version is 3.11 and Linux is Red Hat 8.5
The code that i am trying to run is as follows:
**#Define Global Variables
emp_ids = []
response_results = []
**#Data pull
if response.status_code == 200:
print(f”Successfully connected to UKG API: Status Code {response.status_code}”)
logger.info(‘Successfully connected to UKG API: Status Code {}’.format(response.status_code))
def get_tasks(session):
tasks = []
for emp_id in emp_ids:
tasks.append(asyncio.create_task(session.get(baseURL+f"personnel/v1/user-defined-fields?employeeId={emp_id}", headers=headers)))
return tasks
start_time = time.time()
async def get_data():
session_timeout = aiohttp.ClientTimeout(total=1800)
async with aiohttp.ClientSession(timeout=session_timeout) as session:
tasks = get_tasks(session)
responses = await asyncio.gather(*tasks)
for response in responses:
response_results.append(await response.json(content_type=None))
asyncio.run(get_data())
end_time = time.time()
total_time = end_time - start_time
print(f"It took {total_time} seconds to make {len(emp_ids)} API Calls")
logger.info("It took {} seconds to make {} API Calls".format(total_time,len(emp_ids)))**
#Error
Traceback (most recent call last):
File “/home/pentaho/pdi/Scripts/Python/UKG/UKG_Employee_User_Defined.py”, line 103, in
asyncio.run(get_data())
File “/home/zmadni/anaconda3/lib/python3.11/asyncio/runners.py”, line 190, in run
return runner.run(main)
^^^^^^^^^^^^^^^^
File “/home/zmadni/anaconda3/lib/python3.11/asyncio/runners.py”, line 118, in run
return self._loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/zmadni/anaconda3/lib/python3.11/asyncio/base_events.py”, line 653, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File “/home/pentaho/pdi/Scripts/Python/UKG/UKG_Employee_User_Defined.py”, line 101, in get_data
response_results.append(await response.json(content_type=None))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/zmadni/anaconda3/lib/python3.11/site-packages/aiohttp/client_reqrep.py”, line 1120, in json
return loads(stripped.decode(encoding))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/zmadni/anaconda3/lib/python3.11/json/init.py”, line 346, in loads
return _default_decoder.decode(s)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/zmadni/anaconda3/lib/python3.11/json/decoder.py”, line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/zmadni/anaconda3/lib/python3.11/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)
I had to add content_type=None to make this run in Linux as without it the async call would get stuck and not return anything until 30 min timeout expires. The odd thing is that this Error does not shows up randomly meaning there are days where the script runs fine without any issues? Again, this is my first time dealing with API’s so i apologize if i missed anything.
Zeeshan Madni is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.