Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Jira Server REST API returns 200 response but with an html in response object

Smitha Pasham
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
November 17, 2022

Hi - I'm trying to access JIRA Server REST API using Python. We currently on Jira 9.2.0 Data center version in dev environment and  Jira Server 9.2.0 in Production environment and running code aganist prod or dev produces same errors. Below are the steps I followed to access the API:

1. I have admin access to Jira and created a Personal Access Token under my user profile.

2. I'm using my email address and PAT as the credentials for the BasicAuth

3. I have tried using base64 encoding authentication headers as well

4. URL I'm trying to access is https://jira-dev.mycompany.com/rest/api/2/application-properties (In fact I have tried api/2/project and many other endpoints)

5. When I execute the end point call I receive a 200 status but fails on JSON parsing

6. Printing response.text shows plenty of html code which I believe is actually authentication failing

7. I have tried passing Basic64 encoded credentials in headers as well but still the same error.

8. Blaming it on Python, I have tried from PowerShell and I still get the same error (this is a working piece of code that we are currently using aganist Jira cloud)

Below is my code snippet I have redacted the actual credentials but leaving the format in place so you get the gist.

 

Python using BasicAuth

import requests
from requests.auth import HTTPBasicAuth
import json

url = "https://jira-dev.company.com/rest//api/3/application-properties"

auth = HTTPBasicAuth("fist.last@company.com", "mytoken")
headers = {
'Content-Type': 'application/json'
}

response = requests.get(
url,
headers=headers,
auth=auth
)

print(response.status_code)
#print(response.text)
print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))

Python using encoded credentials

import requests
import base64

text = 'first.last@mycompany.com:mytoken'
text_bytes = text.encode('Ascii')
base64_bytes = base64.b64encode(text_bytes)

url = "https://jira-dev.mycompany.com/rest//api/2/application-properties"
headers = {
'Content-Type': 'application/json',
'Authorization': 'Basic %s' % base64_bytes
}

response = requests.get(
url,
headers=headers
)

print(response.status_code)
print(response.text)

 PowerShell using Basic64 encoding

-----------------------------------------------------------

$Uri= "https://jira-dev.mycompany.com/rest/api/2/application-properties"
$AdminUser = "first.last"
$Token = "mytoken"


# The Header is created with the given information.

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $AdminUser, $Token)))

$Header = @{

Authorization = ("Basic {0}" -f $base64AuthInfo)

}

 # Splat the parameters in a hashtable for readability

$UsersParameters = @{

Method = "GET"

Headers = $Header

Uri = $Uri

}

$response = Invoke-RestMethod @UsersParameters

echo $response

---------------------------------------------------------------------

All three variations produced same result:

Response code of 200, failing to parse JSON elements in response and the response coming back in html.

 

Any help will be greatly appreciated.

 

Thanks,

Smitha

 

 

1 answer

0 votes
robert Mugabuhamye
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 18, 2022

Hello @Smitha Pasham

You should try using your username and password and not the PAT. Check also if you do not have a captcha on jira login page.

 

Regards 

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events