Forum Discussion
Passing information to API, works in CURL, not in PS7
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?
Hello 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.
- AndySvintsSteel Contributor
Hello 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.
- ChrisSchroederCopper ContributorThank you Andy! I had tried that before in previous iterations (when obviously something else was wrong) but somewhere along the way I left it out of my testing. I appreciate your help - it works fine now!!