0

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?

3
  • Try running a man-in-the-middle proxy like Fiddler on your workstation and capture the POST requests from Postman and Powershell - if it works in Postman you can compare that to PowerShell and see exactly what the differences are in the raw HTTP requests they’re sending behind the scenes… Commented Mar 1, 2024 at 9:11
  • thx! nice tool . Problem solved. It was the "/" at the end of the Uri Commented Mar 1, 2024 at 11:21
  • @AntonStruhli Don't forget to answer your own question below :) Commented Mar 1, 2024 at 13:08

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.