Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Invoke-WebRequest Bad Request using Rest API Edited

I'm trying to get the rawcontent using jira rest api using powershell.

(Invoke-WebRequest -credentials get-credential "https://name.jira.com/rest/api/2/search?jql=project%20%3D%20PPM%20AND%20status%20%3D%20%22Waiting%20Approval%22&fields=key").rawcontent

However, after running this script I get Bad Request even when I enter my credentials after getting prompted

 

Invoke-WebRequest : The remote server returned an error: (400) Bad Request.
At line:1 char:2
+ (Invoke-WebRequest -Credential get-credential "https://solutions.frae ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke
-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWe
bRequestCommand


Has anyone encountered this before? The pretty sure the account I used is correct as I used it to login to jira but it doesn't work when I run this script. Main reason for creating this script is I want to get the output of the Issue Keys so I can use it as the parameters for the other script I used.

1 answer

Hi,

Sorry to kick a two year old post. Hopefully you got your answer by now... but since this is pretty high on the most view yet unanswered questions, it's a slow day, and rawcontent had me curious... so here goes:

400 Bad Request means Atlassian doesn't like the way your call is formatted. If it were your credentials, it would definitely return 401 Unauthorized.

From what I can tell, you're missing a few things Atlassian is expecting. First is the Method... should be Get in this case. Your are also missing a couple Headers: Accept and Authorization (which should replace your -Credential tag)... You can't just pass PSCredentials. You need to use a header to tell them you're using Basic auth and you'll need to encode the values as Base64... and use api version 3... 

Give this a go:

$cred = Get-Credential
$Base64Cred = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $cred.GetNetworkCredential().UserName,$cred.GetNetworkCredential().Password)))
$server = 'https://yourinstance.atlassian.net'
$jql = "Project = `"TEST Project`""
#remember to place a ` in front of all " characters

$responce = Invoke-WebRequest "$server/rest/api/3/search?jql=$jql" -Headers @{"Authorization" = "Basic $Base64Cred"; "Accept" = "application/json"} -Method 'Get'
$responce.RawContent

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Posted in Jira Core

How to manage many similar workflows?

I have multiple projects that use variations of the same base workflow. The variations depend on the requirements of the project or issue type. The variations mostly come in the form of new statuses ...

3,957 views 11 5
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you