JSRendar
JSRendar

Reputation: 21

JIRA Cloud API POST Request Error: Invoke-RestMethod : {"errorMessages":["Unexpected character ('a' (code 97)): expected a valid value

I am successful in using JIRA's Cloud Restful API to perform GET and DELETE calls, but I keep running into an error when attempting to add a user to a group using POST.

The same call works correctly in POSTMAN, but not so in Powershell. I suspect it has something to do with how I am creating the Bod.

I tried modifying $Body by encoding the key value pair with no success:

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

$UserAccountID = '8675309'

$Body = @{
     accountId = $UserAccountID
}

$ExternalGroupName = 'external-users'

$AddToExternalGroup = Invoke-RestMethod ('https://aquaman.jira.com/rest/api/3/group/user?groupname=' + $ExternalGroupName + '&accountId=' + $UserAccountID ) -Headers $Headers -Method POST -ContentType "application/json" -Body $Body

$AddToExternalGroup 

I expect the call to go through like it does in postman, but instead I get this error message:

Invoke-RestMethod : {"errorMessages":["Unexpected character ('a' (code 97)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: org.apache.catalina.connector.CoyoteInputStream@5fe6ff6b; line: 1, column: 2]"]}
At line:2 char:31
+ ... rnalGroup = Invoke-RestMethod ('https://aquaman.jira.com/rest/api/3 ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (Method: POST, Reque\u2026application/json
}:HttpRequestMessage) [Invoke-RestMethod], HttpResponseException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Upvotes: 0

Views: 253

Answers (1)

JSRendar
JSRendar

Reputation: 21

Fixed! It was a problem with the $Body after all. After following @AdminOfThings's recommendation of using convertto-Json, what was left was to make sure I did not encase the values in quotes. Thank you!

Upvotes: 2

Related Questions