Invoke-WebRequest ignores content encoding - by Joel 'Jaykul' Bennett

Status : 


Sign in
to vote
ID 807794 Comments
Status Active Workarounds
Type Bug Repros 5
Opened 11/5/2013 10:51:28 AM
Access Restriction Public


Invoke-WebRequest is leaving byte-order marks (BOM) in the Content.

For example, this file:

If I fetch it with WebClient.DownloadString I get the content I expected, but when I fetch it with Invoke-WebRequest, I end up with the byte-order mark in the first few bytes of the Content property (which prevents me from parsing the string as XML or PowerShell etc.). 

If I use Invoke-WebRequest -Outfile (or manually write the Content property with the BOM into a file), I can Get-Content on the file and the BOM is gone, because Get-Content absorbs it correctly.

The root cause of the problem seems to be that when the stream is read, PowerShell isn't using the specified encoding (the CharacterSet specified in the Response Header).
Sign in to post a comment.
Posted by Morozov A. _ on 11/19/2015 at 2:52 AM
I came across a similar mistake.
In my case it turned out to be a working solution to exchange data via a temporary file.

Posted by Keith Hill MVP on 5/13/2015 at 10:11 PM
I was really hoping this would be fixed in V5. I can't see how this could be considered correct behavior. It was breaking some tests I had that fetched XML and was then validating that XML. Those BOM bytes at the start was causing the test to fail.