0
votes

I am trying to send a POST request to Jira with a script and it works perfectly fine with Python but not with PowerShell.

I tried GET request to get some data from Jira using PowerShell and it worked, so I guess login details are correct. So my only concern is the payload. Maybe my JSON formar is incorrect.

Python (works):

headers = {
  "Accept": "application/json;charset=utf-8",
  "Content-Type": "application/json"
}

payload = {
  "fields": {
    "project": {
      "key": "SDF"
    },
    "summary": "test",
    "description": "Creating of an issue using project keys and issue type names using the REST API",
    "issuetype": {
      "name": "Request"
    }
  }
}

password = open('passwords_jira2snow.txt').readlines()
jira_login = HTTPBasicAuth(password[0].rstrip('\n'), password[1].rstrip('\n')) 

r = requests.post(url="domain/rest/api/2/issue", data = json.dumps(payload), auth=jira_login, verify=False, headers=headers)
#jira_response = json.loads(r.text)
print(r.text)

PowerShell (doesn't work):

$payload = ('{
  "fields": {
    "project": {
      "key": "SDF"
    },
    "summary": "test",
    "description": "Creating of an issue using project keys and issue type names using the REST API",
    "issuetype": {
      "name": "Request"
    }
  }
}')
$hdrs1 = @{}
$hdrs1.Add("login",$login)
$hdrs1.Add("password",$password)

$CreateJira = Invoke-RestMethod -Method POST -Headers $hdrs1 -Uri "domain/rest/api/2/issue" -Body $payload -ContentType "application/json"
Write-Host $CreateJira

I'm getting

ERROR: REST call to sessionauth endpoint failed with error 'ProtocolError -
System.Net.WebException: The remote server returned an error: (400) Bad Request.
     at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.GetResponse(WebRequest request)
     at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.ProcessRecord()'
Current endpoint: 'https://domain/sessionauth/v1/authenticate'
2
I believe the 1st header is called username instead of login - Sid
credentials is not the issue. GET request works as I mentioned - Adomas
$payload should probably be a hashtable, not a JSON string. - Ansgar Wiechers
@AnsgarWiechers would you mind giving an example? Not really familiar with hashtables - Adomas
posted an answer, thanks everyone - Adomas

2 Answers

1
votes

Check the documentation inside PS using the cmdlet get-help Invoke-RestMethod -Examples. There's an example on how to handle a POST request. Instead of setting your credentials on the header, create a Credentials object and use the -Credentials parameter of the Invoke-RestMethod.

Credentials are generally handled differently from GET requests than POST Requests on REST.

On the other hand, I recommend that you use this JiraPS Module instead, so you don't need to reinvent the wheel and focus on getting your job done!

0
votes

Not sure why but defining credentials other way helped. It's strange because GET request worked by default.

 $login = "test"
 $password = "test"  
 $pair = "$($login):$($password)"
 $encodedCreds = 
 [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))  
 $hdrs1.Add("Authorization", "Basic $encodedCreds")