Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use Invoke-WebRequest to write binary data to a file and still get the status code?

I need to download a file using a POST request, however, since PowerShell seemingly can't handle binary data in pipes and variables (Out-File doesn't support binary data), I need to use the -OutFile parameter of Invoke-WebRequest. When doing this, however, it no longer outputs the status code so that I can capture it in a variable:

PS K:\hmpf> $Filepath = "E:\shmimpf\g\yyv.fgr"
PS K:\hmpf> $URL = "https://wwwh.ipswich.org/tycobrahe/magazine.php?burgerflipcount=76"
PS K:\hmpf> (Invoke-WebRequest -Uri $URL -OutFile $Filepath -Method Post).StatusCode
PS K:\hmpf> (Invoke-WebRequest -Uri $URL -Method Post).StatusCode
200
PS K:\hmpf>
like image 650
schuelermine Avatar asked Oct 24 '25 21:10

schuelermine


1 Answers

From the documentation for Invoke-WebRequest:

-PassThru

    Indicates that the cmdlet returns the results, in addition to writing them to a file. 
    This parameter is valid only when the OutFile parameter is also used in the command.

So to get the response results in conjunction with -OutFile:

(Invoke-WebRequest -Uri $URL -OutFile $Filepath -Method Post -PassThru).StatusCode
like image 187
Mathias R. Jessen Avatar answered Oct 26 '25 11:10

Mathias R. Jessen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!