Mar 19 2021 02:43 PM - edited Mar 19 2021 02:47 PM
This API call to a Cisco Firepower 1120 works in CURL (on Ubuntu)... please note credentials and host name have been obfuscated here but are correct in the original versions:
curl --cacert fwcert -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ "grant_type": "password", "username": "myaccountname", "password": "mypassword" }' 'https://myhost/api/fdm/v5/fdm/token'
...this API returns a temporary token for use in future API requests. It works precisely as expected with a return 200 response and all the fields I need.
I am attempting to get this to work in PowerShell 7 (I also tried Powershell ISE v5 before I upgraded), with the following seemingly equivalent code:
$param = @{
"grant_type"="password"
"username"="myaccountname"
"password"="mypassword"
} | ConvertTo-Json
$fwurl = 'https://myhost/api/fdm/v5/fdm/token'
Invoke-RestMethod -Uri $fwurl -Method POST -SkipCertificateCheck -Body $param
... and I get the following error:
Invoke-RestMethod: {"message":"Token grant_type is either not-specified or not-supported - null","status_code":400}
Since it works in CURL but not Powershell I'm thinking I'm doing something wrong with the PS script but I can't figure out what it is. Help?
Mar 19 2021 04:08 PM - edited Mar 19 2021 04:10 PM
SolutionHello @ChrisSchroeder,
I think you are missing -ContentType parameter:
If this parameter is omitted and the request method is POST, Invoke-RestMethod sets the content type to application/x-www-form-urlencoded.
Invoke-RestMethod -Uri $fwurl -Method POST -SkipCertificateCheck -Body $param -Content 'application/json'
Reference: Invoke-RestMethod
Hope that helps.
Mar 23 2021 06:18 AM
Mar 19 2021 04:08 PM - edited Mar 19 2021 04:10 PM
SolutionHello @ChrisSchroeder,
I think you are missing -ContentType parameter:
If this parameter is omitted and the request method is POST, Invoke-RestMethod sets the content type to application/x-www-form-urlencoded.
Invoke-RestMethod -Uri $fwurl -Method POST -SkipCertificateCheck -Body $param -Content 'application/json'
Reference: Invoke-RestMethod
Hope that helps.