简体   繁体   中英

How to get HTTP status on Invoke-WebRequest/Invoke-RestMethod when using -OutFile parameter?

I have a request to a restful API (Note, it happens both with Invoke-WebRequest and Invoke-RestMethod ):

$resp1 = Invoke-WebRequest -OutFile $clusterConfigZipFile -Uri $apiFolder -Body $fileContent -Method 'POST' -Credential $adminCredentials -ContentType "application/x-www-form-urlencoded" -ErrorAction SilentlyContinue -Verbose

I know it works ok because:

  • It downloads a zip file that is correct.
  • If I use Fiddler I can see response headers (HTTP 200) 捕获提琴手

My problem is that code is returning nothing in $resp1 . I have read that it is a "feature" when you use -OutFile .

Is there anyway to capture response or at least response code it PowerShell?

As documented , use the -Passthru parameter if you want the response returned in addition to being written to a file.

-OutFile<String>

Saves the response body in the specified output file. Enter a path and file name. If you omit the path, the default is the current location.

By default, Invoke-WebRequest returns the results to the pipeline. To send the results to a file and to the pipeline, use the Passthru parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM