Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,364,824
Community Members
 
Community Events
168
Community Groups

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

Works for me. Many thanks!

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events