簡體   English   中英

Invoke-RestMethod utf8 字符?

[英]Invoke-RestMethod utf8 characters?

我正在從Invoke-RestMethod執行 PUT,並且端點不喜歡我的瑞典字符 åäö。 I have verified from downloaded curl on my Windows CMD window, and got the same results error 400. The strange thing is that it works from PostMan on my machine. 它可能與utf8和BOM有關嗎? 試圖從我的字符串中刪除它,但它沒有幫助。

不工作:

{    "User":  {
                 "email":  "xxxxx@domain.com",
                 "last_name":  "Åkesson",
                 "first_name":  "Thomas",
                 "enabled":  true
             }
}

在職的:

{
    "User":  {
                 "email":  "xxxxx@domain.com",
                 "last_name":  "akesson",
                 "first_name":  "Thomas",
                 "enabled":  true
             }
}

對我來說,問題似乎與將 PowerShell 的默認 output 編碼更改為 UTF-8 相關 或者端點本身可能有一些錯誤?

對我來說,這種方法有效(場景是從 WordPress 中提取的)。 請參閱下面的 Get-Content 'file path' -Encoding UTF8。 也許首先輸出到 XML 是這里的關鍵?

#GET WordPress posts.
Invoke-RestMethod -Uri 'https://wordpress.com/category/feed/' -OutFile C:\PowerShell\MakeFeed.xml
#NOTE -Encoding UTF8 is crucial here / otherwise bad characters for superscript, quotes, etc.
[xml]$Content = Get-Content C:\PowerShell\MakeFeed.xml -Encoding UTF8

嘗試使用 powershell 在團隊中發布內容時,我遇到了丹麥字符 åæø 的問題。 我發現解決方案是在調用中包含字符編碼:

$FilePath = '.\body.txt'
$fileContent = Get-Content -Path $FilePath -Encoding UTF8 
Write-host $fileContent
Invoke-RestMethod -ContentType "application/json; charset=windows-1252" -Uri "https://example.webhook.office.com/webhookb2" -Method 'Post' -Body $fileContent

即包括 charset=windows-1252 對我有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM