Powershell Invoke-RestMethod

13,996 views
Skip to first unread message

CJ Dickinson

unread,
Aug 11, 2016, 4:40:52 PM8/11/16
to CATS API v3
Is there any help I can get with this method of calling the APIs? 

I am trying to Call a candidate to test these new API calls, as I am totally unfamiliar with REST. I have read as much as my brain can handle...

Eventually this will be used to POST and GET backups for our SQL database imports.

I have tried quite a few methods all with no success.

Error: "Invoke-RestMethod : Cannot bind parameter 'Headers'. Cannot convert the "authorization: Token 297fecXXXXXXXXXXXXXXd31eff5" value of type "System.String" to type 
"System.Collections.IDictionary"."

Error: 
Invoke-WebRequest : A positional parameter cannot be found that accepts argument ' Authorization: Token 297fecXXXXXXXXXXXXXXd31eff5'.
At line:4 char:11
+ $result = Invoke-WebRequest -Uri $url  $cred  -Method GET -ContentTyp ...
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeWebRequestCommand



Error:
Invoke-RestMethod : Cannot bind parameter 'Headers'. Cannot convert the "authorization: Token
297fecXXXXXXXXXXXXXXd31eff5" value of type "System.String" to type "System.Collections.IDictionary".
At line:1 char:35
+ ... hod GET  -h 'authorization: Token 297fecXXXXXXXXXXXXXXd31eff5' - ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-RestMethod], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeRestMethodCommand


Error:
At line:3 char:42
+ Invoke-RestMethod -Method GET  -header @{-x = 'authorization: Token 2 ...
+                                          ~
The hash literal was incomplete.
At line:3 char:102
+ ... -x = 'authorization: Token 297fecXXXXXXXXXXXXXXd31eff5'} -Uri ht ...
+                                                                 ~
Unexpected token '}' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : IncompleteHashLiteral
 


Anthony Allan

unread,
Aug 11, 2016, 5:09:04 PM8/11/16
to CATS API v3
Hey CJ,

It looks like your `Invoke-RestMethod` has bad syntax. I don't have a Windows machine in front of me, so I can't test it, but the documentation for `Invoke-RestMethod` says that the `-Headers` flag takes an `IDictionary` not a string (hence the error message you're getting). All those error messages you are getting are from the commands you are using, not from our API.

CJ Dickinson

unread,
Aug 15, 2016, 10:20:59 AM8/15/16
to CATS API v3
$URI= "https://api.catsone.com/v3" $Endpoint = "/candidates/99973949" $FullURL = "$URI$Endpoint" $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" $headers.Add("Authorization",'Token 297fecXXXXXXXXXXXXXXd31eff5') $response = Invoke-RestMethod $FullURL -Headers $headers

Error: Invoke-RestMethod : {"message":"Unsupported Accept."} At line:8 char:13 + $response = Invoke-RestMethod $FullURL -Headers $headers + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

I am so lost... Something isn't passing or accepting the token.

CJ Dickinson

unread,
Aug 15, 2016, 10:23:08 AM8/15/16
to CATS API v3
$e = @{
    Headers = @{"Authorization" = "Token 297fecXXXXXXXXXXXXXXd31eff5"}
}

$response = Invoke-RestMethod @e

ERROR:
Invoke-RestMethod : {"message":"Unsupported Accept."} At line:6 char:13 + $response = Invoke-RestMethod @e + ~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExcepti on + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Anthony Allan

unread,
Aug 15, 2016, 2:23:37 PM8/15/16
to CATS API v3
Hey there,

Looks like you're hitting our API now, which is good. That error means that you are missing a valid `Accept` header. Normally, most clients will add one in automatically sot it's rarely an issue, but it looks like `Invoke-RestMethod` doesn't do that. You'll need to manually add an Accept header the same way you are adding the Authorization header, and it's value should be one of '*/*', 'application/json', or 'application/hal+json'.

I notice that this isn't mentioned in our API docs, so I will make sure to get it added for people who run into the same issue as you.

CJ Dickinson

unread,
Aug 16, 2016, 10:59:09 AM8/16/16
to CATS API v3
Somewhat solved, now I just need to get PS to download or display the response.

$e = @{ Method = "Get" Uri = "https://api.catsone.com/v3/candidates/99973949" Headers = @{ Authorization = "Token 297fecXXXXXXXXXXXXXXd31eff5" Accept = "application/json" } } $response = Invoke-RestMethod @e


Message has been deleted

CJ Dickinson

unread,
Aug 16, 2016, 11:29:47 AM8/16/16
to CATS API v3
Okie Dokie, here it is complete and solved. This will output the file to the users desktop as a text (or whatever you need).


   
    $CanidateID
="99973949"
    $Local
= "$Home\Desktop\$CanidateID.TXT"

    $e
= @{
   
Method  = "Get"

   
Uri     = "https://api.catsone.com/v3/candidates/$CanidateID"
   
Headers = @{
       
Authorization = "Token Token 297fecXXXXXXXXXXXXXXd31eff5"
       
Accept        = "application/json"
   
}
   
}


    $response
= Invoke-RestMethod @e | Out-File -FilePath $Local -Force


Source used for assistance with PS syntax: https://redd.it/4xep1s

Branislav Ballo

unread,
Apr 19, 2023, 2:27:08 AM4/19/23
to CATS API v3
In
"Authorization = "Token Token 297fecXXXXXXXXXXXXXXd31eff5""
line there is "Token" twice. There should be:
"Authorization = "Token 297fecXXXXXXXXXXXXXXd31eff5""

Dátum: utorok 16. augusta 2016, čas: 17:29:47 UTC+2, odosielateľ: CJ Dickinson
Reply all
Reply to author
Forward
0 new messages