Razoll
Razoll

Reputation: 107

Invoke-RestMethod with query parameter string failing (403)

I am trying to simplify my http request, however query parameters seems to cause issues and I cannot figure out what the problem is.

Works:

$endpoint = "https://testendpoint.com/test?param1=value1&param2=value2&apikey=keyvaule"
Invoke-RestMethod -Uri "$endpoint" -Method 'Post'

Fails:

$body = 
@{
    param1='value1';
    param2='value2';
    apikey='keyvalue';
}
$endpoint = "https://testendpoint.com/test"
Invoke-RestMethod -Uri $endpoint -Body $body -Method 'Post'

I have tried several different variations. Any help would be appreciated

Upvotes: 0

Views: 1550

Answers (1)

NicoKlausIT
NicoKlausIT

Reputation: 51

In your working version, you use several parameters. Parameters are usually responsible for specifying your call, such as specifying what format you require for your return (Json and so on).

In the version that is not running, you take the parameters and pass them to your body. However, parameters have no place in the body. There is usually your passing data, like a list of users in a JSON array or something else.

I don't know an option to use parameters smarter than you already do in your working version.

It could help you, if you take a look at the basics of http requests.

I would also recommend an API tool like Postman, there you can get a good start with http requests and where you need what data. Also it has an option to convert your API call to PowerShell code or something else.

Upvotes: 1

Related Questions