Hi @yury beltrikov,
Could you share the HTTP request you’re making? For your reference, here is an example curl command that works to do what you are looking to do:
curl https://api.pagerduty.com/users/PXXXXXX \\
-X PUT \\
-H 'Content-Type: application/json' \\
-H 'Authorization: YOUR_AUTH_INFO' \\
-d '{
"user": {
"name": "The user's name is required",
"email": "The user's email is also required",
"role": "observer",
"type": "user_reference"
}
}'
Hope this helps…
My code is in PowerShell but I do not think it would be an issue as it’s pretty simple:
Step 1. Get the user.
$headers = @{
“Accept”= “application/vnd.pagerduty+json;version=2”;
“Authorization”= “Token token=$APIkey”
“Content-Type” = “application/json”
}
$id = ‘Pxxxxx’ #id of an existing user
$GetAPIUri = “https://api.pagerduty.com/users/$id”
$user = Invoke-RestMethod -Method GET -Uri $GetAPIUri -Headers $headers -Proxy $proxy -ProxyUseDefaultCredentials -UseBasicParsing
Step 2. Update the role field.
$user.user.role = ‘observer’
Step 3. Update the PagerDuty user
$UpdateAPIUri = “https://api.pagerduty.com/users/$id”
$user = Invoke-RestMethod -Method PUT -Uri $UpdateAPIUri -Headers $headers -Body $user -Proxy $proxy -ProxyUseDefaultCredentials -UseBasicParsing
Hi @yury beltrikov,
Ok, that makes sense. And I see why it might seem like a good idea to read in the whole user object, update a field and send back the whole thing. But that won’t work reliably – partly because the user object as returned by GET /users/{id} contains fields that are not permitted in PUT /users/{id} payloads (computed or otherwise immutable fields)
Instead, you should construct a JSON object that contains only the user.name, user.email, user.role and user.type.
I see. I slightly modified the code.
Step 1. Get the user.
$headers = @{
“Accept”= “application/vnd.pagerduty+json;version=2”;
“Authorization”= “Token token=$APIkey”
“Content-Type” = “application/json”
}
$id = ‘Pxxxxx’ #id of an existing user
$GetAPIUri = “https://api.pagerduty.com/users/$id”
$user = Invoke-RestMethod -Method GET -Uri $GetAPIUri -Headers $headers -Proxy $proxy -ProxyUseDefaultCredentials -UseBasicParsing
Step 2. Modify role in a new user object
$updated_user = @{
‘Name’ = $user.user.name
‘email’ = $user.user.email
‘role’ = ‘user’ #this is a change. Current role = observer
}
Step 3. Update the PagerDuty user.
$UpdateAPIUri = “https://api.pagerduty.com/users/$id”
$user = Invoke-RestMethod -Method PUT -Uri $UpdateAPIUri -Headers $headers -Body $updated_user -Proxy $proxy -ProxyUseDefaultCredentials -UseBasicParsing
However, I get the following error.
Invoke-RestMethod : {“error”:{“message”:“Arguments Caused Error”,“code”:2002}}
At line:1 char:9
Hi @yury beltrikov,
Looks like in your Step 2 you are setting a key ‘Name’ but it should be ‘name’ and you aren’t setting ‘type’ which should be ‘user_reference’. If I am correctly inferring PowerShell syntax from your snippet, the following should be closer to what you need:
$updated_user = @{
'name' = $user.user.name
'email' = $user.user.email
'role' = 'user'
'type' = 'user_reference'
}
Hope this helps, let me know what you observe…
Thanks,
Martin
@martin Thank you!
I was able to change the user role via API with the changed code
Nice work @yury beltrikov, glad you got it working!