2

I am trying to use Invoke-Restmethod in Powershell to call an API (I'm fairly new to this). I can get it to POST and return the jwt access token. I can also use that token to return an id via GET, however I'm then having trouble with the next step of returning the next set of data. I can get it to work manually via curl. I believe the issue may be because multiple headers are required to return the tenant list and I'm unsure of the format to get this to work.

The curl script looks as follows, and works as expected:

curl -XGET -H "Authorization: Bearer <jwt access token>" -H "ID: <id>" https://theapiurl.com/.......

I've tried multiple ways to do this in powershell, most recently as below, but nothing I'm trying works. I've tried returning the individual $headers contents and building a string (i.e. $headers2 = $.headers.Item(Authorization) + ......) but that doesn't work either. To be honest, I've tried so many different things I've forgotten what I have and haven't tried

$headers = @{
    'ID' = $id
    'Authorization' = $auth_string
    }
    
$response = Invoke-RestMethod -Method Get -Headers $headers -Uri $url

Please could you let me know the correct way to add multiple headers (which I think is the problem and what I'm getting wrong)?

3
  • 2
    What you’ve got with a hashtable (@{ ... }) should be fine. Are you sure your $id and $auth_string values are correct? What error message / response code are you getting?
    – mclayton
    Commented Mar 27, 2021 at 9:14
  • Not sure if i'm fully understanding what you're trying to do, but if you need to add an additional property to a hashtable after its been declared, use the add method: $headers.add('name','value')
    – Mark Wragg
    Commented Mar 27, 2021 at 9:34
  • In this answer I suggested to use a session variable where you can add multiple headers. QA
    – Alex_P
    Commented Mar 27, 2021 at 20:44

2 Answers 2

2

In case it's useful to anyone else, another syntax for setting the parameters of this commandlet is as follows (real working example for uploading to the GitHub release repository). It's usful to set all the switches (without prepending a hyphen) in the parameters object like so:

  $upload_params = @{
    Uri = $upload_uri + "?name=$asset_name"
    Method = 'POST'
    Headers = @{
      'Authorization' = "token $github_token"
      'Accept' = 'application/vnd.github.everest-preview+json'
    }
    InFile = $asset
    ContentType = 'application/octet-stream'
  }
"Uploading $asset..."
$upload = Invoke-RestMethod @upload_params
"The server returned:"
echo $upload

The variable $upload contains the full object returned from the server (converted from json to a PowerShell object). So, for example, you can also get properties of this obect like so:

"Upload successfully posted as " + $upload.url 
0

Thanks for all the responses - none of them were really the answer but they did give me the confidence I was doing it the right way.

I'd been using PS Write-Host to check the data returned - this was working for the tokens and ID's, but wasn't working for next step. I wasn't getting an error, just no data. (I did see the returned data when testing manually in a command prompt window). As soon as I added an -OutFile to the PS and checked the file, I realised it was working all along and PS just wasn't showing me the results. 2 hours wasted, although I've learnt more as a result!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.