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
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!
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
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!