I’m trying to update the status of tests in a Jira test plan. There are 10 tests linked to test execution ‘RO-6665’. I want to change their status from ‘TODO’ to ‘PASS’.
But I’m getting this error: ‘Failed to retrieve issue details. Status code 401’. I’ve double-checked all tokens and access info.
Here’s a simplified version of what I’m trying to do:
import requests
JIRA_SERVER = 'my_server_url'
JIRA_USER = 'my_username'
JIRA_API_TOKEN = 'my_api_token'
test_execution = 'RO-6665'
new_status = 'PASS'
issue_url = f'{JIRA_SERVER}/rest/api/2/issue/{test_execution}'
headers = {'Content-Type': 'application/json'}
auth = (JIRA_USER, JIRA_API_TOKEN)
response = requests.get(issue_url, headers=headers, auth=auth)
if response.status_code != 200:
print(f'Error: {response.status_code}')
else:
# Update status logic here
print('Status updated successfully')
Any ideas on why I’m getting a 401 error? How can I fix this to update the test statuses?
The 401 error suggests an authentication issue. Have you tried using a personal access token (PAT) instead of an API token? PATs are generally more robust for API interactions. Also, ensure your JIRA_SERVER URL includes the ‘/jira’ path if it’s a self-hosted instance.
Another approach is to use the Jira Python library (jira) which handles authentication more seamlessly. It might resolve your issue:
from jira import JIRA
jira = JIRA(server=JIRA_SERVER, basic_auth=(JIRA_USER, JIRA_API_TOKEN))
issue = jira.issue(test_execution)
for test in issue.fields.issuelinks:
test_issue = jira.issue(test.outwardIssue.key)
jira.transition_issue(test_issue, new_status)
This method often bypasses common authentication hurdles. If it still fails, check your account permissions in Jira as you might lack the necessary rights to modify test plans.
hey markseeker91, have u tried using a session object? it might help with auth issues. like this:
session = requests.Session()
session.auth = (JIRA_USER, JIRA_API_TOKEN)
response = session.get(issue_url, headers=headers)
also check if ur account has the right permissions in jira. sometimes that can cause 401 errors too. good luck!
I’ve encountered similar issues with Jira’s API before, and a 401 error generally points to an authentication problem. It might help to check that your API token is still valid as tokens can expire, and confirm that you’re using the correct authentication method since some Jira instances require OAuth rather than basic authentication. It’s also important to ensure the JIRA_SERVER URL is formatted correctly with https:// and doesn’t include any unnecessary trailing slashes. For cloud instances, the username should usually be your email address, and you should verify that your account has proper permissions to modify test plans. If these steps don’t resolve the issue, try using a persistent connection with requests.Session() and enable debug logging to get more detailed error information.