Forums

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

JIRA API with Powershell

Niall Collins
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!
January 28, 2019

Hi all,

I have run in to an issue with JQL in Powershell using the 'Invoke-WebRequest' commandlet. The comment I am using (below) works in Browser, and it works in Postman (with basic auth), however it consistently returns 400 Bad Request. And I'm wondering where I am going wrong.

Command:

Invoke-WebRequest -Uri 'https://site.mysite.com/issues/rest/api/2/search?jql=project%20%3D%20TEAM%20AND%20fixVersion%20%3D%20%22Version%20%To20Check%20For%22' -Credential $cred

The above command has been anonymised, but otherwise it works in browser and Postman. I've messed around with formatting thinking at various times that it might be spacing, quotation marks & other caveats. However, I have ruled out most of those. 

I am sure I am missing something basic. The JQL is correct, because it is copied directly from a JIRA Url showing the report I'm seeking to ingest as data in Powershell.

Any help would be appreciate.

Thanks!

Niall.

2 answers

8 votes
Chander Inguva
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.
January 28, 2019

Hey Collins,

 

Your REST API url is wrong

Please use https://<jira_base_url>/rest/api/2/search?jql='bla bla'

 

For working example please find attached code snippet

$user = 'username'
$pass = 'password'

$pair = "$($user):$($pass)"

$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))

$basicAuthValue = "Basic $encodedCreds"

$Headers = @{
Authorization = $basicAuthValue
}
Invoke-WebRequest -Uri 'https://<jira_base_url>/rest/api/2/search?jql=project = CIT AND issuetype = "JIRA Request" AND (created >= startOfDay(-30d) OR updated >= startOfDay(-30d))' `
-Headers $Headers

 

For documentation:

https://docs.atlassian.com/software/jira/docs/api/REST/7.6.1/#api/2/search-search

 

Hope this helps

Thanks

Chander Inguva

1 vote
Lee Correll
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.
February 14, 2019

@Niall Collins, In short:

Remove the portion of your URL that has "/issues" in it.

So go from 

https://site.mysite.com/issues/rest/api/2/search?blahblahblah

to

https://site.mysite.com/rest/api/2/search?blahblahblah.

Also, you can (should, maybe?) replace that "2" with "latest".

Suggest an answer

Log in or Sign up to answer