VMware Cloud Community
future2000
Enthusiast
Enthusiast
Jump to solution

Leveraging the Horizon REST API through PowerCLI

I've been using some functions from the following blogger to access the Horizon REST API

https://andrewmorgan.ie/2019/09/horizon-7-10-rest-api/

This works fine when reading from the API. I am however unable to post/put anything into Horizon using Invoke-RestMethod -Post or -Put

Please note the following is hand written as the system is offline

Example

$domainAccounts = Invoke-RestMethod -Method Get -Uri "$url/rest/config/v1/ic-domain-accounts" -ContentType "Application/Json -Headers (Get-HRHeader -accesstoken $accesstoken)

$domainAccountsJson = $domainaccounts | ConvertTo-Json

$domainAccounts = Invoke-RestMethod -Method Post -Uri "$url/rest/config/v1/ic-domain-accounts" -ContentType "Application/Json -Headers (Get-HRHeader -accesstoken $accesstoken) -body ($domainAccounts)

This code always results in

Invoke-RestMethod : The remote server returned an error: (400) Basd request

Perform exactly the same work in POSTMAN is successfull.

Can anyone help me understand how my approach is wrong. Cheers

 

0 Kudos
1 Solution

Accepted Solutions
future2000
Enthusiast
Enthusiast
Jump to solution

Hey big thanks Lucd, appreciate you taking the time to respond. I've just figured it out and I'm embarrassed to admit to the fact I just wasn't paying enough attention to the swagger UI. I expected what came out from my GET would be what was required in my PUT. This clearly wasn't the case. Modification of the JSON payload in PowerCLI to match the requirements and hey presto. It works. Thanks!

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

Bad request (400) most of the time means the payload (Body) is in an incorrect format.
Can you compare what is actually sent (something like WireShark for example) with the Invoke-RestMethod and with Postman?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

future2000
Enthusiast
Enthusiast
Jump to solution

Hey big thanks Lucd, appreciate you taking the time to respond. I've just figured it out and I'm embarrassed to admit to the fact I just wasn't paying enough attention to the swagger UI. I expected what came out from my GET would be what was required in my PUT. This clearly wasn't the case. Modification of the JSON payload in PowerCLI to match the requirements and hey presto. It works. Thanks!

0 Kudos