0

I got a program from a formerly colleague and now should maintain it. This python script asks our Jira instance with a given jql ( on the API ). The return is a list of all issues, which are matching the search criteria. But now it's not working, and I receive on the server ( Ubuntu ) and on my local windows PC a Json error message. note : it ran for about a year not, but back then it worked.

Here is what the script looks like :

import json
import subprocess

jiraSerachUrl = "https://ourJiraInstance.net/rest/api/2/search?jql=key%20=%20%22TEST-123%22"
jiraResponse = subprocess.Popen(["curl","-l","-s","-u", "jiraUser"+":"+"jiraUserPassword", "-X", "GET", jiraSerachUrl ],stdout=subprocess.PIPE,shell=True).communicate()[0]
## shell=True only added for Windows Instance
print(type(jiraResponse))
##print =  <class 'bytes'>
print(jiraResponse)
## print = b''
jiraJsonResponse = json.loads(jiraResponse.decode('utf-8'))
print(jiraJsonResponse)

The jql/jira search address returns the following (shorted answer, all fields of the task are returned):

{"expand":"names,schema","startAt":0,"maxResults":50,"total":1,"issues": [{"expand":"operations,versionedRepresentations,editmeta,changelog,transitions,renderedFields", "id":"145936","self":"https://ourJiraInstance.net/rest/api/2/issue/145936","key":"TEST-123","fields":{"parent": ...

The Error on the Windows PC is the following

Traceback (most recent call last): File "C:\Users\User\Desktop\test.py", line 10, in jiraJsonResponse = json.loads(jiraResponse.decode('utf-8')) File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\lib\json__init__.py", line 319, in loads return _default_decoder.decode(s) File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\lib\json\decoder.py", line 339, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\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)

This is the error on the Ubuntu Server ( running the same script )

Traceback (most recent call last): File "searchJira.py", line 33, in jiraJsonResponse = json.loads(jiraResponse) File "/usr/lib/python2.7/json/init.py", line 338, in loads return _default_decoder.decode(s) File "/usr/lib/python2.7/json/decoder.py", line 366, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/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

So far I tried to change the Json load to simpleJson, but with the same result. Changing the format to which Json should decode ( e.g. unicode ) took no effect.

Twiebie
  • 412
  • 1
  • 7
  • 16
  • Is there any output from print(jiraResponse) or it is empty or None object? The error message indicates that the object jiraResponse may be None. – ichbinblau Dec 02 '16 at 06:06
  • the output is in the comment below -> b'' – Twiebie Dec 02 '16 at 06:08
  • Then I think the root cause is that the object to be decoded is empty or None. It leads to a ValueError. If there is truly no data returned from jql. Then make a logical judgement to avoid the error. `if jiraJsonResponse: jiraJsonResponse = json.loads(jiraResponse.decode('utf-8')) print(jiraJsonResponse)` – ichbinblau Dec 02 '16 at 06:14
  • Looks like the bytestring is causing the error. try to encode the byte string before you use. In this case you can do `jiraJsonResponse.encode('utf-8')` and then use it for other `json` operations. – Varad Dec 02 '16 at 06:22
  • @Theresa - the return should not be empty. when I access the webpage there is an issue class serialized. – Twiebie Dec 02 '16 at 06:31
  • @Varad - when I try to encode JiraJsonResponse, befor using it i get "NameError: name 'jiraJsonResponse' is not defined" when I try to encode the jiraResponse befor printing it, i get the error "AttributeError: 'bytes' object has no attribute 'encode'" – Twiebie Dec 02 '16 at 06:32
  • Then you will have to check whether you can get data thru curl. Probably you can use curl command `curl -l -s -u "jiraUser:jiraUserPassword" -X GET "https://ourJiraInstance.net/rest/api/2/search?jql=key%20=%20%22TEST-123%22"` to check the result. Check there is any proxy in between or other network issues. – ichbinblau Dec 02 '16 at 06:35
  • I have tried a bit and finaly got it. by replacing curl with responses i got finally the result I wanted. my request looks now like this : `r = requests.get(jiraSerachUrl,auth=HTTPBasicAuth(user, password), verify=False) jiraJsonResponse=json.loads(r.text)` – Twiebie Dec 02 '16 at 07:51

1 Answers1

0

I have tried a bit and finaly got it. by replacing curl with responses i got finally the result I wanted. my request looks now like this :

r = requests.get(jiraSerachUrl,auth=HTTPBasicAuth(user, password), verify=False) 
jiraJsonResponse=json.loads(r.text)
Twiebie
  • 412
  • 1
  • 7
  • 16