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'
usernameinstead oflogin- Sid$payloadshould probably be a hashtable, not a JSON string. - Ansgar Wiechers