JoeRod
JoeRod

Reputation: 921

Powershell Invoke-RestMethod incorrect character

I'm using Invoke-RestMethod to get page names from an application I'm using. I notice that when I do a GET on the page it returns the page name like so

This page â is working

However the actual page name is

This page – is working

Here's how my request looks

 Invoke-WebRequest -Uri ("https://example.com/rest/api/content/123789") -Method Get -Headers $Credentials -ContentType "application/json; charset=utf-8"

The problem is with the en-dash, does anyone know how I can fix this?

Upvotes: 7

Views: 17155

Answers (3)

Gromuls
Gromuls

Reputation: 41

One line solution (without files):

[system.Text.Encoding]::UTF8.GetString((Invoke-WebRequest "https://www.example.com").RawContentStream.ToArray())

Upvotes: 4

Harald F.
Harald F.

Reputation: 4753

Invoke-restmethod or invoke-webrequest?

The Invoke-RestMethod cmdlet uses the default decoding on the result of the HttpWebResponse.CharacterSet property.

If that is not set it uses a default encoding of ISO-8859-1 by default (afaik).

I'm assuming your server is sending some wrong charset in the response headers (or dropping it) hence it's beeing decoded wrongly.

Do you know what charset/encoding are sent in your response from your server?

If you're trying the Invoke-webrequest; check your headers in your response like e.g.

$r = invoke-webrequest http://example.com
$r.Headers

If you're dealing with an encoding issue; e.g. your server is not sending the right headers; you can always try to dump the response in a file and read it with a different encoding:

Invoke-WebRequest http://example.com -outfile .\test.txt
$content = get-content .\test.txt -Encoding utf8 -raw

In this case you will no longer be working with the http-response; but it might help you debug/find the encoding issues your looking for.

Upvotes: 3

vitrilo
vitrilo

Reputation: 1517

In case of Invoke-WebRequest does not detect responce encoding right, you can use RawContentStream and convert it to needed encoding:

$resp = Invoke-WebRequest -Uri ... $html=[system.Text.Encoding]::UTF8.GetString($resp.RawContentStream.ToArray());

Upvotes: 16

Related Questions