Relay a binary file with PowerShell Invoke-WebRequest

287 Views Asked by At

I'm retrieving a small image from a remote web site. My goal is to relay that image from memory to an API without resorting to writing a local copy.

$jpgParams = @{
   'Uri' = 'https://i.stack.imgur.com/iEQ9h.jpg?s=256&g=1'
   'Method' = 'GET'
   'Headers' = @{ 'Cache-Control' = 'no-cache' }
}
[Net.ServicePointManager]::SecurityProtocol = 'tls12, tls11'
$jpgResponse = Invoke-WebRequest @jpgParams

This stores a byte array of the image in $jpgResponse.Content. I can write it to local storage to verify that it's good.

[io.file]::WriteAllBytes('c:\temp\tom.jpg',$jpgResponse.Content)

Ultimately, I don't need a local copy. How do I transform the byte array that's available in memory and pass it along via Invoke-WebRequest? I know file can be processed with -Infile, but that's what I'm hoping to avoid.

$postParams = @{
   'Uri' = 'https:\\antijerry.com\api\v1\profilepic?name=tom.jpg'
   'Method' = 'POST'
   'ContentType' = 'image/jpeg'
}
$postResponse = Invoke-WebRequest @postParams -Body ?
0

There are 0 best solutions below