0

New to Powershell here so any advice is appreciated! I'm Posting to this website's API (I code-named it authenticate.com in the below code) to receive in the response an auth token as a cookie. The next objective is to take the cookie and use it to validate to a different API. How can I capture the auth-token returned by the first API and save it into a variable?

My code:

$Url = 'https://authenticate.com/apikeylogin'
$auth = @{
     keyPublic= '********************'
     keySecret= '********************'
}
$json = $auth | ConvertTo-Json
$response = Invoke-WebRequest $Url -Method Post -Body $json -ContentType 'application/json'
$response | Get-Member
$response.RawContent

The response in raw-text:

HTTP/1.1 200 OK
auth-token: ******************
[Below this is are a dozen more lines of raw data]

To restate the question, how do I get the above value of 'auth-token' and store it into a variable?

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • from what i have read, usually you use `Invoke-RestMethod` instead of the web page oriented `Invoke-WebRequest` cmdlet. the `I-RM` stuff auto-converts your returned data to a structured object if it gets JSON back. that is fairly easy to get info out of ... [*grin*] – Lee_Dailey Jun 15 '20 at 01:51
  • To see what members and data your object has, you can pipe it to `Select-Object` like this: `$response | Select-Object -Property *`. – stackprotector Jun 15 '20 at 06:21
  • Does this answer your question? [How to parse JSON from the Invoke-WebRequest in PowerShell?](https://stackoverflow.com/questions/37762615/how-to-parse-json-from-the-invoke-webrequest-in-powershell) – Theo Jun 15 '20 at 12:54
  • @Lee-Daily I originally tried Invoke-RestMethod. The issue was that it hid too much of the response and I couldn't see the auth-token that I needed! Thanks – miguelsantos10 Jun 15 '20 at 14:30
  • @Theo I saw this before posting my question! I think it is similar to my question and has been of some help but I was not able to have success following it's logic. I am still using it as reference though as I seek to solve this problem! Thanks! – miguelsantos10 Jun 15 '20 at 14:36
  • The `$response` variable will most likely also have a `Content` property (you could see that with `$response | Format-List *`. This should be in JSON format. Try `$obj = $response.Content | ConvertFrom-Json; $token = $obj.auth-token`. – Theo Jun 15 '20 at 14:41

2 Answers2

0

You can use Select-String (similar to grep in powershell) to figure out the line containing the auth-token and get the auth-token value.

$response = "HTTP/1.1 200 OK `
auth-token: ABCDEFGHIJKLMNOP `
[Below this is are a dozen more lines of raw data]"

$authTokenline =
 $response.Split("`n") | Select-String -Pattern "^auth-token:.*$" 
$authToken = $authTokenline.ToString().Split(":")[1]
ABCDEFGHIJKLMNOP
Venkataraman R
  • 12,181
  • 2
  • 31
  • 58
  • thanks for the answer! i tried this and i am getting the error: you cannot call a method on a null-valued expression. CategoryInfo: InvalidOperation (:) [], RuntimeException, FullyQualifiedErrorId: InvokeMethodOnNull. I am currently working through this error ! – miguelsantos10 Jun 15 '20 at 15:03
0

Ok, I have solved the problem using .Substring()

$auth = @{
     keyPublic= '********************'
     keySecret= '********************'
}
$json = $auth | ConvertTo-Json
$response = Invoke-WebRequest $Url -Method Post -Body $json -ContentType 'application/json'
$raw = $response.RawContent
$string = $raw | Out-String
$auth_token = $string.Substring(35, 90)

The access token I am requesting from the API always has the same length so I used the substring method to pinpoint exactly which chars from the string are what I need and then store them in the variable "auth-token"