Powershell | API PUT METHOD

40 Views Asked by At

I'm trying to figure out why my script not working anymore:

$Header = @{
            'user' = "myuser"
            'password' = "mypassword"
    
      
        }
    Invoke-RestMethod  -Uri "https://mywebsite/api/user/login" -Headers $Header #|fl content  | Out-String
    
    $response = Invoke-RestMethod -Uri "https://mywebsite/api/user/login" -Headers $Header
    $token = $response.content | Select-Object -ExpandProperty apiKey

$Body = @{
    'companyId' = "399"
    'branches' = "COMPANY_ONLY"
    'active' = "ACTIVE_ONLY"
    'servers' = "SERVERS_ONLY"
}

$Parameters = @{
    Method = "Put"
    Headers = @{
        "apitoken" = "$token"
        "Content-Type" = "application/json"

    }
    Uri = "https://mywebsite/api/pcs/"
    Body = ($Body | ConvertTo-Json)
    ContentType = "application/json"
}

$response = Invoke-RestMethod @Parameters

I got this error

"Invoke-RestMethod: The remote server returned an error: (403) Forbidden."

I tried with Postman and it works! But with Powershell not.

Other methods like Post and Get are working, just the Put Method not.

Any Ideas?

1

There are 1 best solutions below

0
Anton Struhli On

Problem solved: It was the "/" at the end of the Uri