SOLVED

Passing information to API, works in CURL, not in PS7

%3CLINGO-SUB%20id%3D%22lingo-sub-2223810%22%20slang%3D%22en-US%22%3EPassing%20information%20to%20API%2C%20works%20in%20CURL%2C%20not%20in%20PS7%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2223810%22%20slang%3D%22en-US%22%3E%3CP%3EThis%20API%20call%20to%20a%20Cisco%20Firepower%201120%20works%20in%20CURL%20(on%20Ubuntu)...%20please%20note%20credentials%20and%20host%20name%20have%20been%20obfuscated%20here%20but%20are%20correct%20in%20the%20original%20versions%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CEM%3Ecurl%20--cacert%20fwcert%20-X%20POST%20--header%20'Content-Type%3A%20application%2Fjson'%20--header%20'Accept%3A%20application%2Fjson'%20-d%20'%7B%20%22grant_type%22%3A%20%22password%22%2C%20%22username%22%3A%20%22myaccountname%22%2C%20%22password%22%3A%20%22mypassword%22%20%7D'%20'https%3A%2F%2Fmyhost%2Fapi%2Ffdm%2Fv5%2Ffdm%2Ftoken'%3C%2FEM%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E...this%20API%20returns%20a%20temporary%20token%20for%20use%20in%20future%20API%20requests.%26nbsp%3B%20%26nbsp%3BIt%20works%20precisely%20as%20expected%20with%20a%20return%20200%20response%20and%20all%20the%20fields%20I%20need.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20attempting%20to%20get%20this%20to%20work%20in%20PowerShell%207%20(I%20also%20tried%20Powershell%20ISE%20v5%20before%20I%20upgraded)%2C%20with%20the%20following%20seemingly%20equivalent%20code%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24param%20%3D%20%40%7B%3CBR%20%2F%3E%22grant_type%22%3D%22password%22%3CBR%20%2F%3E%22username%22%3D%22myaccountname%22%3CBR%20%2F%3E%22password%22%3D%22mypassword%22%3CBR%20%2F%3E%7D%20%7C%20ConvertTo-Json%3C%2FP%3E%3CP%3E%24fwurl%20%3D%20'https%3A%2F%2Fmyhost%2Fapi%2Ffdm%2Fv5%2Ffdm%2Ftoken'%3C%2FP%3E%3CP%3EInvoke-RestMethod%20-Uri%20%24fwurl%20-Method%20POST%20-SkipCertificateCheck%20-Body%20%24param%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E...%20and%20I%20get%20the%20following%20error%3A%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CEM%3EInvoke-RestMethod%3A%20%7B%22message%22%3A%22Token%20grant_type%20is%20either%20not-specified%20or%20not-supported%20-%20null%22%2C%22status_code%22%3A400%7D%3C%2FEM%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESince%20it%20works%20in%20CURL%20but%20not%20Powershell%20I'm%20thinking%20I'm%20doing%20something%20wrong%20with%20the%20PS%20script%20but%20I%20can't%20figure%20out%20what%20it%20is.%26nbsp%3B%20Help%3F%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2223810%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAPI%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2223958%22%20slang%3D%22en-US%22%3ERe%3A%20Passing%20information%20to%20API%2C%20works%20in%20CURL%2C%20not%20in%20PS7%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2223958%22%20slang%3D%22en-US%22%3E%3CP%3EHello%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1003092%22%20target%3D%22_blank%22%3E%40ChrisSchroeder%3C%2FA%3E%2C%3C%2FP%3E%3CP%3EI%20think%20you%20are%20missing%20%3CSTRONG%3E-ContentType%3C%2FSTRONG%3E%20parameter%3A%3C%2FP%3E%3CP%3E%3CSPAN%3EIf%20this%20parameter%20is%20omitted%20and%20the%20request%20method%20is%20POST%2C%26nbsp%3B%3C%2FSPAN%3EInvoke-RestMethod%3CSPAN%3E%26nbsp%3Bsets%20the%20%3CSTRONG%3Econtent%20type%20to%26nbsp%3B%3C%2FSTRONG%3E%3C%2FSPAN%3E%3CSTRONG%3Eapplication%2Fx-www-form-urlencoded.%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3EInvoke-RestMethod%20-Uri%20%24fwurl%20-Method%20POST%20-SkipCertificateCheck%20-Body%20%24param%20-Content%20'application%2Fjson'%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EReference%3A%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Fmicrosoft.powershell.utility%2Finvoke-restmethod%3Fview%3Dpowershell-7.1%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EInvoke-RestMethod%3C%2FA%3E%3C%2FP%3E%3CP%3EHope%20that%20helps.%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

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? 

 

2 Replies
best response confirmed by ChrisSchroeder (New Contributor)
Solution

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.

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