having probems translating curl to Invoke-RestMethod @Params

Rob merritt 6 Reputation points
2022-12-08T13:58:22.15+00:00

I am having problems using the invoke-restmethod any ideas what i am missing as I try and replicate this curl command

curl -d '{"sam_account_name":"testuser"}' -H "Content-Type: application/json" -H "Authorization: Bearer <API-KEY>" -X POST https://test.local/api/v3/uids/

$token = "<API_KEY>"  
$Params = @{  
Uri = 'https://test.local/api/v3/uids/'  
Method = "POST"  
Headers = @{  
ContentType = 'application/json'  
Authorization = '<API_KEY>'  
}  
Body = '{"sam_account_name":"testuser"}'  
}  

$response1 = Invoke-RestMethod @Params  

I GET:
Invoke-RestMethod : {"detail":"Authentication credentials were not provided."}
At line:13 char:14

  • $response1 = Invoke-RestMethod @Params
  • ~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
  • FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

$token = "<API_KEY>"  
$props = @{  
    Uri = 'https://test.local/api/v3/uids/'  
    Method = "POST"  
    ContentType = 'application/json'  
    Body = '{"sam_account_name":"testuser"}'  
    Headers = '{"Authorization: Bearer <API_KEY>"}'   
}  
$Response2 = Invoke-RestMethod @props  

I GET:
Invoke-RestMethod : Cannot bind parameter 'Headers'. Cannot convert the "{"Authorization: Bearer <API-KEY>"}" value of type "System.String" to type
"System.Collections.IDictionary".
At line:9 char:32

  • $Response2 = Invoke-RestMethod @props
  • ~~~~~~
  • CategoryInfo : InvalidArgument: (:) [Invoke-RestMethod], ParameterBindingException
  • FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2022-12-08T16:02:39.297+00:00

    Shouldn't the header be Authorization = "Bearer <API_KEY>"

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.