[英]Powershell Invoke-WebRequest and character encoding
I am trying to get information from the Spotify database through their Web API. 我试图通过他们的Web API从Spotify数据库中获取信息。 However, I'm facing issues with accented vowels (ä,ö,ü etc.)
但是,我遇到重音元音(ä,ö,ü等)的问题
Lets take Tiësto as an example. 让我们以蒂斯托为例。 Spotify's API Browser can display the information correctly: https://developer.spotify.com/web-api/console/get-artist/?id=2o5jDhtHVPhrJdv3cEQ99Z
Spotify的API浏览器可以正确显示信息: https : //developer.spotify.com/web-api/console/get-artist/?id=2o5jDhtHVPhrJdv3cEQ99Z
If I make a API call with Invoke-Webrequest
I get 如果我使用
Invoke-Webrequest
进行API调用, Invoke-Webrequest
得到
Ti??sto
TI?STO
as name: 作为名称:
function Get-Artist {
param($ArtistID = '2o5jDhtHVPhrJdv3cEQ99Z',
$AccessToken = 'MyAccessToken')
$URI = "https://api.spotify.com/v1/artists/{0}" -f $ArtistID
$JSON = Invoke-WebRequest -Uri $URI -Headers @{"Authorization"= ('Bearer ' + $AccessToken)}
$JSON = $JSON | ConvertFrom-Json
return $JSON
}
How can I get the correct name? 如何获得正确的名字?
Jeroen Mostert , in a comment on the question, explains the problem well: Jeroen Mostert在对该问题的评论中很好地解释了这个问题:
The problem is that Spotify is (unwisely) not returning the encoding it's using in its headers.
问题在于,Spotify(不明智地)没有返回其标头中使用的编码。 PowerShell obeys the standard by assuming ISO-8859-1 , but unfortunately the site is using UTF-8 .
PowerShell通过假定ISO-8859-1遵守该标准 ,但不幸的是 , 该站点使用的是UTF-8 。 (PowerShell ought to ignore standards here and assume UTF-8, but that's just like, my opinion, man.) More details here , along with the follow-up ticket.
(PowerShell应该在这里忽略标准并采用UTF-8,但这就像我的看法一样。) 这里有更多详细信息,以及后续票据。
A workaround that doesn't require the use of temporary files is to re-encode the incorrectly read string . 不需要使用临时文件的解决方法是重新编码不正确读取的string 。
If we assume the presence of a function convertFrom-MisinterpretedUtf8
,we can use the following: 如果我们假设存在一个功能
convertFrom-MisinterpretedUtf8
,则可以使用以下代码:
$JSON = convertFrom-MisinterpretedUtf8 (Invoke-WebRequest -Uri $URI ...)
See below for the function's definition. 有关功能的定义,请参见下文。
convertFrom-MisinterpretedUtf8
: convertFrom-MisinterpretedUtf8
: function convertFrom-MisinterpretedUtf8([string] $String) {
[System.Text.Encoding]::UTF8.GetString(
[System.Text.Encoding]::GetEncoding(28591).GetBytes($String)
)
}
The function converts the incorrectly read string back to bytes based on the mistakenly applied encoding (ISO-8859-1) and then recreates the string based on the actual encoding (UTF-8). 该函数根据错误应用的编码(ISO-8859-1)将错误读取的字符串转换回字节,然后根据实际编码(UTF-8)重新创建字符串。
Issue solved with the workaround provided by Jeron Mostert. Jeron Mostert提供的解决方法解决了问题。 You have to save it in a file and explicit tell Powershell which Encoding it should use.
您必须将其保存在文件中,并明确告诉Powershell它应使用哪种编码。 This workaround works for me because my program can take whatever time it needs (regarding read/write IO)
这种解决方法对我有用,因为我的程序可能需要任何时间(就读/写IO而言)
function Invoke-SpotifyAPICall {
param($URI,
$Header = $null,
$Body = $null
)
if($Header -eq $null) {
Invoke-WebRequest -Uri $URI -Body $Body -OutFile ".\SpotifyAPICallResult.txt"
} elseif($Body -eq $null) {
Invoke-WebRequest -Uri $URI -Headers $Header -OutFile ".\SpotifyAPICallResult.txt"
}
$JSON = Get-Content ".\SpotifyAPICallResult.txt" -Encoding UTF8 -Raw | ConvertFrom-JSON
Remove-Item ".\SpotifyAPICallResult.txt" -Force
return $JSON
}
function Get-Artist {
param($ArtistID = '2o5jDhtHVPhrJdv3cEQ99Z',
$AccessToken = 'MyAccessToken')
$URI = "https://api.spotify.com/v1/artists/{0}" -f $ArtistID
return (Invoke-SpotifyAPICall -URI $URI -Header @{"Authorization"= ('Bearer ' + $AccessToken)})
}
Get-Artist
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.