Skip to content

Commit 722f961

Browse files
authored
Merge pull request #105 from lipkau/fix/#101-SupportUnicode
Fix encoding of Invoke-WebRequest response
2 parents 95c22c4 + 89e3eba commit 722f961

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

ConfluencePS/Private/Invoke-Method.ps1

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,21 @@ function Invoke-Method {
5858
Throw $exception
5959
}
6060

61-
# pass input to local variable
62-
# this allows to use the PSBoundParameters for recursion
63-
$_headers = $Headers
64-
6561
# Add Basic Authentication to Header
6662
$SecureCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(
6763
$('{0}:{1}' -f $Credential.UserName, $Credential.GetNetworkCredential().Password)
6864
))
69-
$_headers += @{
70-
"Authorization" = "Basic $($SecureCreds)"
71-
'Content-Type' = 'application/json; charset=utf-8'
65+
$_headers = @{
66+
"Accept" = "application/json"
67+
"Accept-Charset" = "utf-8"
68+
"Authorization" = "Basic $($SecureCreds)"
69+
"Content-Type" = "application/json; charset=utf-8"
7270
}
71+
72+
# Append the Headers passed into the local variable _headers
73+
# the variable used for the headers must be different from the parameter to allow
74+
# the use of PSBoundParameters for recursion
75+
$Headers.Keys.foreach( {$_headers[$_] = $Headers[$_]})
7376
}
7477

7578
Process {
@@ -84,6 +87,10 @@ function Invoke-Method {
8487
$GetParameters = $null
8588
}
8689

90+
# load DefaultParameters for Invoke-WebRequest
91+
# as the global PSDefaultParameterValues is not used
92+
$PSDefaultParameterValues = $global:PSDefaultParameterValues
93+
8794
# set mandatory parameters
8895
$splatParameters = @{
8996
Uri = $URi
@@ -97,11 +104,6 @@ function Invoke-Method {
97104
# http://stackoverflow.com/questions/15290185/invoke-webrequest-issue-with-special-characters-in-json
98105
if ($Body) {$splatParameters["Body"] = [System.Text.Encoding]::UTF8.GetBytes($Body)}
99106

100-
# load DefaultParameters for Invoke-WebRequest
101-
# as the global PSDefaultParameterValues is not used
102-
# TODO: find out why PSJira doesn't need this
103-
$script:PSDefaultParameterValues = $global:PSDefaultParameterValues
104-
105107
# Invoke the API
106108
try {
107109
Write-Verbose "[$($MyInvocation.MyCommand.Name)] Invoking method $Method to URI $URi"
@@ -118,11 +120,13 @@ function Invoke-Method {
118120
# Test HEADERS if Confluence requires a CAPTCHA
119121
$tokenRequiresCaptcha = "AUTHENTICATION_DENIED"
120122
$headerRequiresCaptcha = "X-Seraph-LoginReason"
121-
if (
122-
$webResponse.Headers[$headerRequiresCaptcha] -and
123-
($webResponse.Headers[$headerRequiresCaptcha] -split ",") -contains $tokenRequiresCaptcha
124-
) {
125-
Write-Warning "Confluence requires you to log on to the website before continuing for security reasons."
123+
If ($webResponse.Headers) {
124+
if (
125+
$webResponse.Headers[$headerRequiresCaptcha] -and
126+
($webResponse.Headers[$headerRequiresCaptcha] -split ",") -contains $tokenRequiresCaptcha
127+
) {
128+
Write-Warning "Confluence requires you to log on to the website before continuing for security reasons."
129+
}
126130
}
127131
}
128132

@@ -154,7 +158,7 @@ function Invoke-Method {
154158
else {
155159
if ($webResponse.Content) {
156160
# API returned a Content: lets work wit it
157-
$response = ConvertFrom-Json -InputObject $webResponse.Content
161+
$response = ConvertFrom-Json ([Text.Encoding]::UTF8.GetString($webResponse.RawContentStream.ToArray()))
158162

159163
if ($null -ne $response.errors) {
160164
Write-Verbose "[$($MyInvocation.MyCommand.Name)] An error response was received from; resolving"
@@ -192,8 +196,8 @@ function Invoke-Method {
192196

193197
# Self-Invoke function for recursion
194198
$parameters = @{
195-
URi = "{0}{1}" -f $response._links.base, $response._links.next
196-
Method = $Method
199+
URi = "{0}{1}" -f $response._links.base, $response._links.next
200+
Method = $Method
197201
Credential = $Credential
198202
}
199203
if ($Body) {$parameters["Body"] = $Body}

Tests/ConfluencePS.Integration.Tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ InModuleScope ConfluencePS {
237237
$Title3 = "Pester New Page from Object"
238238
$Title4 = "Pester New Page with Parent Object"
239239
$RawContent = "Hi Pester!"
240-
$FormattedContent = "<p>Hi Pester!</p>"
240+
$FormattedContent = "<p>Hi Pester!</p><p>👋</p>"
241241
$pageObject = New-Object -TypeName ConfluencePS.Page -Property @{
242242
Title = $Title3
243243
Body = $FormattedContent
@@ -323,7 +323,7 @@ InModuleScope ConfluencePS {
323323
$Title3 = "Pester Test Space Home"
324324
$Title4 = "orphan"
325325
$Title5 = "*orphan"
326-
$Content = "<p>Hi Pester!</p>"
326+
$Content = "<p>Hi Pester!</p><p>&eth;&Yuml;&lsquo;&lsaquo;</p>"
327327
(Get-ConfluenceSpace -SpaceKey $SpaceKey).Homepage | Add-ConfluenceLabel -Label "important" -ErrorAction Stop
328328
Start-Sleep -Seconds 20 # Delay to allow DB index to update
329329

0 commit comments

Comments
 (0)