我正在讀取一個 HTML 檔案,需要將它作為字串引數傳遞給 JSON 呼叫。HTML 中有用于 css/styles 的花括號。我正在看這個問題,但我的更多是關于 JSON 特殊字符,其中一個是其他轉義字符。該檔案也可能有 [] 字符。
$HtmlFromFile2 = @"
<html>
<style type="text/css">.LetterList {font-family:"arial"}</style>
<body>
To [firstname]:
This is a normal body with special JSON characters.
</body>
</html>
"@
$HtmlFromFile2Stripped = $HtmlFromFile2.Replace("`r", "").Replace("`n", "").Replace("`t","")
#$HtmlFromFileFixed = % { [System.Text.RegularExpressions.Regex]::Unescape($HtmlFromFile2Stripped) }
$HtmlFromFileFixed = $HtmlFromFile2Stripped.Replace("{","\{").Replace("}","\}")
Write-Host "After RegEx Fix"
Write-Host $HtmlFromFileFixed
$templateID = "Test_Temp7"
$body = @"
{ "uuid": "$templateID",
"subject": "My Email Subject",
"body": "$HtmlFromFileFixed"
}
"@
# This also tests if our JSON is "well-formed", on extra or missing commas, braces, brackets, etc...
$bodyJSON = ConvertFrom-Json $body
# Write-Host $body
$params = @{
Uri = 'https://stage-tms.demo.com/templates/email'
Headers = $headers
Method = 'POST'
Body = "$body"
ContentType = 'application/json'
}
#Invoke-RestMethod @params
我使用 Convert-FromJSON 作為測驗。當它起作用時,通常 Invoke-RestMethod 會起作用。當 Convert-FromJSON 失敗時,Invoke-RestMethod 將回傳 http statsu=400 和 statusDescription="Bad Request"。
當前錯誤:
ConvertFrom-Json : Invalid object passed in, ':' or '}' expected. (137): { "uuid": "FEMAImport_Home_Test_Temp7",
"subject": "SBA Disaster Loan Assistance TestText",
"body": "<html> <style type="text/css">.sbaLetterList \{font-family:"arial"\}</style><body> This is a
normal body with special JSON characters. </body></html>"
}
At C:\Scripts\TestJSONEscapeBraces.ps1:38 char:13
$bodyJSON = ConvertFrom-Json $body
~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : NotSpecified: (:) [ConvertFrom-Json], ArgumentException
FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand
Update 1: I did some more tests. I'm ending up with too may permutations of what works and what doesn't work. I can't include the entire HTML file I'm using; I need a technique to isolate errors and identify them. I was hoping ConvertFrom-JSON would do that for me.
From below, you can see different files I'm trying. The first one (the real file and the larger one fails both on the ConvertFrom-JSON and the REST/Post. The other two are now working.
$HtmlBodyFilename = "$PSScriptRoot\Emailbody_Home.html"
#$HtmlBodyFilename = "$PSScriptRoot\Emailbody_Home_SimpleTest.html"
#$HtmlBodyFilename = "$PSScriptRoot\Emailbody_Home_SimpleTestWithBraces.html"
$HtmlFromFile = Get-Content $HtmlBodyFilename -Raw #the -Raw option gets a string instead of an array of lins
$HtmlFromFileStripped = $HtmlFromFile.Replace("`r", "").Replace("`n", "").Replace("`t","")
I'm now able to read and process a small simple test file with curly braces in it. But when I point to the real HTML file on disk, I get errors, and don't know how to isolate it. I was probably assuming it was the curly brace.
If I use the ConvertFrom-JSON and the real HTML file, I get: ConvertFrom-Json : Invalid object passed in, ':' or '}' expected. (143): { "uuid": "FEMAImport_HomeFile_Test_Temp3", "subject": etc...
Now, if I remove the "ConvertFrom-JSON" and use the real HTML File, I get this error from the SOAP request: "The request was aborted: The connection was closed unexpectedly." Of course one difference is that the file is much bigger, about 83KB.
Before now, I have been getting a lot of 400 "Bad Request" errors, which is a useless error. I need to know what is wrong with the request.
uj5u.com熱心網友回復:
讓 Powershell 為您完成所有作業,不要自己構建 JSON 字串,使用物件:
$body = ConvertTo-Json @{
uuid = $templateID
subject = "My Email Subject"
body = Get-Content $HtmlBodyFilename -Raw
}
所有特殊字符都將自動轉義。
歸功于@Hazrelle,當然在這種情況下您甚至不必轉換任何內容,就像Invoke-WebRequest為您做的那樣:
$params = @{
Uri = 'https://stage-tms.demo.com/templates/email'
Headers = $headers
Method = 'POST'
Body = @{
uuid = $templateID
subject = "My Email Subject"
body = Get-Content $HtmlBodyFilename -Raw
}
ContentType = 'application/json'
}
uj5u.com熱心網友回復:
您甚至不需要將任何內容轉換為 Json。Invoke-WebRequest會做的。
$body = @{
uuid: $templateID,
subject: "My Email Subject",
body: $HtmlFromFileFixed
}
Invoke-WebRequest ... -Body $body
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/328031.html
標籤:powershell
