I have the following PowerShell script:
$CURLEXE = 'C:\Windows\System32\curl.exe'
$URL1 = "Example.com"
$GetTokenCurl = '-s', '-v', '-X', 'POST',
$URL1,
'-H', 'Content-Type: application/x-www-form-urlencoded',
'-d', 'grant_type=client_credentials&client_id=12345678&client_secret=abcdefgh',
'--retry', '2',
'--retry-connrefused',
'--retry-delay', '3'
Write-Host "Command took" (Measure-Command {$GetTokenResponse = & $CURLEXE @GetTokenCurl}).TotalSeconds "Seconds"
$GetTokenJSON = ConvertFrom-Json -InputObject $GetTokenResponse -ErrorAction SilentlyContinue
$SessionToken = $GetTokenJSON.access_token
$AuthBearer = "Authorization: Bearer " + $SessionToken
It gets my Access Token from the SSO server and stores it into the $AuthBearer variable so I can reuse it for later scripts.
My response looks something like this:
{"access_token":"987654321","token_type":"Bearer","expires_in":3600}
I am trying to also validate the HTTP response code from the server, which I have only been able to do (so far) by enabling -v
(verbose) output. I am specifically looking for the HTTP response code after < HTTP/1.1
(i.e. 200). I can see this in the Verbose output:
< HTTP/1.1 200 OK
I can't for the life of me find a way to store the JSON response in a variable and check the contents of the Verbose output.
I've tried and failed to write the Verbose output to a text file. I've tried the following two switches, but these mess up my JSON response:
-i, --include Include protocol response headers in the output -I, --head Show document info only
I also tried the --output
to a file, but that just wrote my JSON response and not the Verbose response to a file:
--output <file> Write to file instead of stdout
Is it possible using PowerShell and cURL to get the JSON response and check for the HTTP response code?