Search code examples
pythonjira-rest-apijsondecoder

Python Jira Rest Api code generates invalid string


I am trying to connect to Jira Rest Api using Python and trying to save data into text file but i am getting huge string returned as response which seems to be invalid string and it also contains html tags.

I am trying to connect Jira using Api token, projectkey and url. Do i need to provide User, Passoword authentication also ?

Below is my code:

import base64
import os
from jira import JIRA
import sys
import requests
import json
import shutil

os.environ["HTTPS_PROXY"] = "XXXX"

# Base encode email and api token
cred =  "Basic " + base64.b64encode(b'[email protected]:XXXX').decode("utf-8")
# Set header parameters
headers = {
   "Accept": "application/json",
   "Content-Type": "application/json",
   "Authorization" : cred
}

# Enter your project key here
projectKey = "TEST"

# Update your site url
url = "https://jira-test.net/secure/RapidBoard.jspa?rapidView=194&projectKey=" + projectKey

# Send request and get response
response = requests.request(
   "GET",
   url,
   headers=headers
)

# Decode Json string to Python
json_data = json.loads(response.text)

# Display issues
for item in json_data["issues"]:
    print(item["id"] + "\t" + item["key"] + "\t" +
        item["fields"]["issuetype"]["name"] + "\t" +
        item["fields"]["created"]+ "\t" +
        item["fields"]["creator"]["displayName"] + "\t" +
        item["fields"]["status"]["name"] + "\t" +
        item["fields"]["summary"] + "\t"
        )

Solution

  • According to your comment the Jira API is returning some HTML code, meaning you probably have an error in your response.

    You need to check if you get a 200 response from the API.

    Replace

    # Decode Json string to Python
    json_data = json.loads(response.text)
    

    by

    # Check for API errors
    response.raise_for_status()
    
    # Decode Json string to Python
    json_data = response.json()