Getting both Error 300 and Error 400 back when creating a new user with PowerShell against V2 API

I have 2 bits of code in play here. The first creates a Jason Web Token. It works fine. I have a second, working bit of code that can pull back a userlist from our Zoom instance no problem with the JWT code. However, trying to add a new user is throwing weird errors

$token = Get-MyJWT -Algorithm ‘HS256’ -type ‘JWT’ -Issuer -SecretKey -ValidforSeconds 30
$headers = @{“Authorization” = “Bearer $token” }
$body = @"
{
“action”: “create”,
“user_info”: {
“email”: “$email”,
“type”: “2”,
“first_name”: “$firstname”,
“last_name”: “$lastname”,
“password”: “$defaultpass”
}
}
"@ | ConvertFrom-Json

$returncode = Invoke-RestMethod -Headers $headers -Method Post -Uri https://api.zoom.us/v2/users -Body $body -ContentType ‘application/json’

When I run that I get back an error 300. However when I interrogate the $Error object, it says I really got back an error 400 about invalid json body. What am I doing wrong here?

Example output:

Invoke-RestMethod : {“code”:300,“message”:“Request Body should be a valid JSON object.”}

Response : StatusCode: 400, ReasonPhrase: ‘Bad Request’, Version: 1.1, Content: System.Net.Http.HttpConnection+HttpConnectionResponseContent, Headers:…

Hey @jmccrae,

Two thoughts, I am wondering if the quotes around the body object (after the @ are turning the whole object into a string?

Maybe try this?

$body = @
{
  "action": "create",
  "user_info": {
    "email": "$email",
    "type": "2",
    "first_name": "$firstname",
    "last_name": "$lastname",
    "password": "$defaultpass"
  }
}
@

My other thought (could just be a weird copy/paste thing) the double quotes in your JSON body are not valid JSON quotes.

Example,
10%20PM

Try this,

{
  "action": "create",
  "user_info": {
    "email": "$email",
    "type": "2",
    "first_name": "$firstname",
    "last_name": "$lastname",
    "password": "$defaultpass"
  }
}

20%20PM

Let me know if this works,
-Tommy

Thanks for the pointer @tommy

Here’s what ultimately worked:

            $body = @{
                action =  "create"
                user_info = @{
                    email = "$email"
                    type = 2
                    first_name = "$firstname"
                    last_name = "$lastname"
                    password = "$defaultpass"
                }
            } | ConvertTo-Json

Thanks again, you sent me in the right direction.

1 Like

You are welcome, glad I was able to help!

Thanks for posting what worked!

-Tommy