R Texas
R Texas

Reputation: 45

How to get curl results as variable in PowerShell

With Powershell, I want to run the following command and store the status_url as a variable. I am unable to reference the status_url directly though.

$upload = curl.exe -u username:password -i -F [email protected] https://sitename.com/csv

$upload stores the following info:

PS C:\Scripts> $upload HTTP/2 200 content-type: text/html; charset=utf-8 strict-transport-security: max-age=31536000; includeSubDomains content-security-policy: script-src 'self' 'unsafe-inline' 'unsafe-eval' https://cdnjs.cloudflare.com https://cdn.ravenjs.com https://cdn.mxpnl.com https://ssl.google-analytics.com http s://.googleapis.com https://cdn.segment.com https://d2yyd1h5u9mauk.cloudfront.net https://.typeform.com;connect-src 'self' https://api.mixpanel.com https://web.delighted.com https://a pi.segment.io;object-src 'self';style-src 'self' 'unsafe-inline' https://cdnjs.cloudflare.com https://use.fontawesome.com https://fonts.gstatic.com https://fonts.googleapis.com;font-src 'self' https://cdnjs.cloudflare.com https://use.fontawesome.com https://fonts.gstatic.com; x-xss-protection: 1; mode=block x-content-type-options: nosniff referrer-policy: strict-origin set-cookie: session=d9337cbf2c313e12_5c24fe8c.Bg33P1e8TOQs9sDCpRWwOoSDDao; Domain=.rolepoint-qa.com; Secure; HttpOnly; Path=/ x-cloud-trace-context: 2586e15ea742ba3202516d6c9c58d3d2;o=1 date: Thu, 27 Dec 2018 16:32:12 GMT server: Google Frontend content-length: 94 expires: Thu, 27 Dec 2018 16:32:12 GMT cache-control: private

{"result": {"status_url": "https://sitename.com/csv/v1/status/1234567"}}

How can I just reference the status_url itself so I can use it in a GET to check the upload status?

The output is JSON, do I need to parse $upload or is there a way to reference $upload.status_url

Upvotes: 3

Views: 11138

Answers (1)

Mike Twc
Mike Twc

Reputation: 2355

I think ($upload | ConvertFrom-Json).result.status_url is not working because of -i parameter, it's mixing response body with headers, so you are not getting valid json. Is it OK to remove it? If you need to keep it then probably use Invoke-Webrequest instead. You can try the code below (I can't test it). If it's working then just browse $response object (response body will be in $response.Content).

$password = ConvertTo-SecureString "yourpassword" -AsPlainText -Force
$user = "yourUser"

$params = @{ 
 Uri = "https://sitename.com/csv"
 InFile = "path/to/your/file.csv"
 Method = "Post"
 Credential = [PSCredential]::new($user, $password)
 }

$response = Invoke-Webrequest @params

You can also try extract that url with regexp from that mixed response, something like:

$Upload -match 'https://sitename.com/csv/v1/status/\d*'
$Matches[0]

Upvotes: 2

Related Questions