Download File from REST API

Product: PowerShell Universal
Version: 2.9.2

I have a Test TXT File with content:
123
zzz
yxy
äöü

The Code:

$ImageData = [IO.File]::ReadAllBytes(“c:\psu\test.txt”)
New-PSUApiResponse -ContentType ‘image/jpg’ -Data $ImageData

The Output:
in Powershell 5 and 7:
123
zzz
yxy
äöü

I have german os language and etc.
First i try the png/jpg file example but with no success.

How do i download files on a non english installation?

1 Like

you need to make sure that each part of the flow, are handeling it with correct encoding.
see also this: powershell - Encoding of the response of the Invoke-Webrequest - Stack Overflow
also look at this:
Webcmdlets should parse the attribute for the correct encoding if not in http header · Issue #3267 · PowerShell/PowerShell (github.com)
it’s not an easy task your trying to do :wink:

After hours with try and search…

[text.encoding]::utf8.getstring([text.encoding]::default.GetBytes(($OutputRestAPI | convertto-json))) | ConvertFrom-Json

What is the encoding in the text file?
I guess not UTF8 ?

The file is UTF8 … but the encoding from Default to UTF8 works with same chars but not for all chars…
it is close…but there are chars like ß with errors.

To Many Encodings…

Get the Invoke-RestMethod output and decode with 28591.

$encoding2 = [System.Text.Encoding]::GetEncoding(28591)
[IO.File]::WriteAllBytes(“c:\psu\TestfileRest.txt”, $encoding2.GetBytes($OutputREST))

and works with other files too.