Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 942
JoeRod Avatar asked Mar 08 '16 03:03

JoeRod


Video Answer


3 Answers

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());

like image 124
vitrilo Avatar answered Oct 17 '22 02:10

vitrilo


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.

like image 23
Harald F. Avatar answered Oct 17 '22 03:10

Harald F.


One line solution (without files):

[system.Text.Encoding]::UTF8.GetString((Invoke-WebRequest "https://www.example.com").RawContentStream.ToArray())
like image 21
Gromuls Avatar answered Oct 17 '22 04:10

Gromuls