Powershell Invoke-RestMethod incorrect character

余生长醉 提交于 2021-02-07 12:53:20

问题


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?


回答1:


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




回答2:


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.




回答3:


One line solution (without files):

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


来源:https://stackoverflow.com/questions/35858490/powershell-invoke-restmethod-incorrect-character

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!