0

I am fetching a JWT for authentication using REST calls. I am new to scripting but I manage to get the token with:

$params = @{"@type"="login";
 "username"="username";
 "password"="password"; 
}
Invoke-WebRequest -Uri http://[SERVER]:[PORT]/api/jwt/login -Method POST -Body $params

But how can I save the Content of the response which contains the token to a parameter which I later on can use in the header for later call?

Br, Patrik

Patrik Falck
  • 15
  • 1
  • 5
  • You can save the content just like in any other programming and scripting language : by sticking it in a variabl (just like you did with the variable $params) – bluuf Dec 14 '18 at 15:13

1 Answers1

1

First you have to save the response:

$res = Invoke-WebRequest -Uri http://[SERVER]:[PORT]/api/jwt/login -Method POST -Body $params

Then you can check what the response contains by typing $res. Your access token will probably be accessible by something like:

($res.Content | ConvertFrom-Json).access_token

$res.Content takes the actual content from the response you get. Then you might want to convert it (usually from JSON as in the example) and access the specific property using .property_name.

As an alternative, you can try to use Invoke-RestMethod which gives you content as object (keep in mind that it might behave differently depending how the service handles authentication).

Robert Dyjas
  • 4,979
  • 3
  • 19
  • 34
  • Thank you! Looks like you are on to something here. Now I get ConvertFrom-Json : Invalid JSON-primitiv: eyJhbGci0i...(token)............. AT C:\getJWT.ps1:13 char 22 + ($response.Content | ConvertFrom-Json).access_token + ~~~~~~~~~~~~~~ +CategoryInfo : NotSpecified: (:) [ConvertFrom-Json9, ArgumentException +FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonComman – Patrik Falck Dec 14 '18 at 15:25
  • That's why I wrote `something like` before the actual code. I have no idea what's the structure of the response you get. If you want me to have a look you must post structure of `$res.Content` in your **question** (not in the comment) – Robert Dyjas Dec 14 '18 at 15:35