Keno Gabriel
Keno Gabriel

Reputation: 19

How to capture output value of API request with PowerShell

I am currently doing some basic API REST test with powershell.

However, I am having an issue with capturing a specific output data

For example:

$bearer = Invoke-RestMethod -Method POST -Body $body -uri "https://api.yourwebsite.com/oauth/token"

Output:

access_token
------------
{longtokenhere}

But when using it with the header:

$header = @{Authorization = "Bearer "+$bearer}

Output is:

Name                           Value
----                           -----
Authorization                  Bearer @{access_token={longtokenhere}}

I would like to know how will I be able to remove the " @{access_token=}" part so I can just use the {longtokehere} only?

Upvotes: 0

Views: 1432

Answers (1)

Keno Gabriel
Keno Gabriel

Reputation: 19

I did some testing and it turns out I can directly call out the access_token output:

When I did this part:

$bearer = Invoke-RestMethod -Method POST -Body $body -uri "https://api.yourwebsite.com/oauth/token"

Output:

access_token
------------
{longtokenhere}

I just simpley used this one:

$bearer.access_token

And I got the direct output of the token only:

{longtokenhere}

My final command worked with this:

Invoke-RestMethod -Method GET -Header @{Authorization = "Bearer "+$bearer.access_token} -ContentType "application/json" -uri "https://api.yourwebsite.com/release/releaseID"

Upvotes: 1

Related Questions