Wrong encoding from - Invoke-WebRequest

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 2 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
Kilsgaard
Posts: 25
Last visit: Fri Jan 26, 2024 11:37 am
Has voted: 1 time

Wrong encoding from - Invoke-WebRequest

Post by Kilsgaard »

Hi
I am in the need of, some serious help :-)

I am working on a small PowerShell GUI, that gets data from a couple of WEB services..
but when I get the data, it is not in the right encoding, it should be UTF8 but it is not when Invoke-WebRequest have been handling it.
I can se that there is a lot of posts about this on the internet, but I have not had any success fixing the encoding on the UTF8 data that is getting back from the webservices..

Could anyone help me with an alternative way of getting the data or an idear how I fix the data when invoke-webrequest have delivered them in a bad format.
I have tried with the different ways with getbytes and loops..
but it does not fix my encoding :-(

thanks on adwance
/Graves
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Wrong encoding from - Invoke-WebRequest

Post by jvierra »

You have to accept the encoding of the web service. Save it to a file and convert the file.

IWR returns an object that has the raw data embedded. Different web services return different objects. The tezt in the object properties will be as inserted by the web service and in that encoding.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Wrong encoding from - Invoke-WebRequest

Post by jvierra »

You probably want to use New-WebServiceProxy. IWR is used to grab web pages and not to call a web service.
User avatar
Kilsgaard
Posts: 25
Last visit: Fri Jan 26, 2024 11:37 am
Has voted: 1 time

Re: Wrong encoding from - Invoke-WebRequest

Post by Kilsgaard »

Hi

Thanks for the idea's.

Got it working writing a file, and reading it again..

It was the quick fix, but I will try work with the New-WebServiceProxy when I modify my code :-)

/Graves
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Wrong encoding from - Invoke-WebRequest

Post by jvierra »

JUst note that what you get back from "Invoke-WebRequest" is an object and cannot be displayed as text. If you output to a file the web page is converted to text so it will be readable. The issue is as I noted above, the objects text from the remote server does not have a BOM so it won't convert correctly but the Invoke-Command has teh ability to use the object to detect the encoding and convert it correctly in most cases.

Again, WebRequest is not the correct command to use for a web service. You want to use the correct command and all of this goes away. The other issue is that you have not given any indication of why you are using an HTML browser emulator to query a web service. Most of the object is useless and the "contents" property contains the web service response as plain text and in the correct encoding for PowerShell.
This topic is 2 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked