当我在django shell上执行我的python函数时,它执行得很好。但是当通过 crontab 执行函数时,它会给出"无法解码 JSON 对象"错误。
successfully made the api call. response status is 200
Exception occured : No JSON object could be decoded
Failed to complete cronjob at ('0,30 * * * *', 'myapp.cron.pulldata', ' >> /usr/local/devops/myapp/crontab.log')
Traceback (most recent call last):
File "/usr/local/devops/provider_monitoring/env/lib/python2.7/site-packages/django_crontab/crontab.py", line 145, in run_job
func(*job_args, **job_kwargs)
File "/usr/local/devops/provider_monitoring/provider_monitoring/provider_automation/cron.py", line 49, in pulldata_and_createjira
response = response.json()
File "/usr/local/devops/provider_monitoring/env/lib/python2.7/site-packages/requests/models.py", line 866, in json
return complexjson.loads(self.text, **kwargs)
File "/opt/python-2.7.10/lib/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/opt/python-2.7.10/lib/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/opt/python-2.7.10/lib/python2.7/json/decoder.py", line 384, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
我正在我的代码中进行 POST 调用以挥霍 rest api。
payload = {'search' : 'search index="xyz" | table a , b , c' , 'earliest_time' : '-60m' , 'output_mode' : 'json_rows' }
url = "https://localhost:8089/services/search/jobs/export"
headers = {'content-type': "application/x-www-form-urlencoded",'authorization': "xyz",'cache-control': "no-cache"}
response = requests.adapters.HTTPAdapter(pool_connections=10, pool_maxsize=10, max_retries=5, pool_block=False)
response = requests.request("POST", url, data=payload, headers=headers, verify=False)
response = response.json()
只有当我在我的 cron 中运行 python 函数时,我才会收到错误。
我正在使用Python版本:2.7.10姜戈 : 1.8
当您没有来自 API 的响应时,会出现"无法解码 JSON 对象"错误。
即使我也有同样的问题。我正在对 splunk 进行 REST API 调用,我想收集每 30 分钟生成的summary_index数据。所以我的 cron 每 30 分钟运行一次。但是每 30 分钟,API 仍在聚合数据,因此每 30 分钟失败一次。当我以时间延迟(即正确聚合数据的时间(点击 api 时,我得到了响应。
所以现在我的 cron 是这样的:
5-59/30 * * * *
此 cron 将在 5 分钟后运行,35 分钟后运行