Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!
cancel
Showing results for 
Search instead for 
Did you mean: 
Richard_Smith_Uchotski
Contributor II
Contributor II

{"jsonrpc":"2.0","method":"OnConnected","params":{"qSessionState":"SESSION_CREATED"}} Response Error

I have written this powershell script, but keep getting this as my response:  {"jsonrpc":"2.0","method":"OnConnected","params":{"qSessionState":"SESSION_CREATED"}}

But I need to get the correct response object. 

The reason I need this to work, is I need to be able to call from Powershell the createobject so I can create an odagnlink in my app, in the ui, after moving the link to the back end. I do not know if this code is correct, it seems to create a connection fine, send, and receive ok, I get a response but it is not the right one.

Am I missing some steps? I tried different payloads but always get the same response! Can someone please help me?

# Setting variables
$serverName = 'myserver.com'
$certPathAndName = "\\\Cert\dev.pfx"
$domainAndUsername = "INTERNAL\sa_scheduler"

# Connecting to the Qlik Sense Server
function Connect-ToQlikServer {
param (
[string]$ServerName,
[string]$CertPathAndName,
[string]$DomainAndUserName
)

$cert = Get-PfxCertificate -FilePath $CertPathAndName
Connect-Qlik -ComputerName $ServerName -UserName $DomainAndUserName -TrustAllCerts -Certificate $cert
}

Write-Host "`nConnecting to Qlik Server"
Connect-ToQlikServer -ServerName $serverName -CertPathAndName $certPathAndName -DomainAndUserName $domainAndUsername

# Connecting to the Qlik Engine
function Connect-QlikSenseEngine {
param (
[Parameter(Mandatory=$true)]
[string]$ServerName,
[Parameter(Mandatory=$true)]
[string]$CertificatePath,
[string]$AppId = ""
)

# Create a WebSocket object
$webSocket = New-Object System.Net.WebSockets.ClientWebSocket

# Add certificate
$cert = Get-PfxCertificate -FilePath $CertificatePath
$certAddedValue = $webSocket.Options.ClientCertificates.Add($cert)

# Construct the WebSocket URL
$engineUrl = "wss://$($ServerName):4747/app/$($AppId)"

# Connect to the WebSocket endpoint
$connectionTask = $webSocket.ConnectAsync($engineUrl, [System.Threading.CancellationToken]::None)
$connectionTask.Wait()

# Return the WebSocket object
return $webSocket
}

Write-Host "`nConnecting to Qlik Engine connection`n"

$webSocket = Connect-QlikSenseEngine -ServerName $serverName -CertificatePath $certPathAndName -AppId 'c45096cc-ab19-48f3-b1d1-4beada5afeb4'

Write-Output $webSocket

# Sending object to Qlik Server to the Qlik Engine
$request = @{
"handle"= 1
"method"= "GetObjects"
"params"= @{
"qOptions"= @{
"qTypes"= @("sheet")
"qIncludeSessionObjects"= $false
"qData"= {}
}
}
}

Write-Host "`Sending request to Qlik Engine`n"

function Send-QlikSenseRequest {
param(
[Parameter(Mandatory=$true)]
[System.Net.WebSockets.ClientWebSocket]$WebSocket,
[Parameter(Mandatory=$true)]
[object]$RequestObject
)

# Convert RequestObject to JSON
$requestJson = $RequestObject | ConvertTo-Json

# Convert requestJson to byte array
$buffer = [System.Text.Encoding]::UTF8.GetBytes($requestJson)

# Send the request
$sendTask = $WebSocket.SendAsync([System.ArraySegment[byte]]$buffer, [System.Net.WebSockets.WebSocketMessageType]::Text, $true, [System.Threading.CancellationToken]::None)
$sendTask.Wait()

return $sendTask
}

Send-QlikSenseRequest -WebSocket $webSocket -RequestObject $request

# Processing the response object from Qlik Server to the Qlik Engine
Write-Host "`nProcessing the Response`n"

function Receive-QlikSenseRequest {
param(
[Parameter(Mandatory=$true)]
[System.Net.WebSockets.ClientWebSocket]$WebSocket
)

$receiveBuffer = New-Object Byte[] 10240 # Increased buffer size
$result = $WebSocket.ReceiveAsync([System.ArraySegment[Byte]]$receiveBuffer, [System.Threading.CancellationToken]::None).Result
$response = [System.Text.Encoding]::UTF8.GetString($receiveBuffer, 0, $result.Count)

return $response
}

Receive-QlikSenseRequest $webSocket

# Closing the connection to the Qlik Engine
Write-Host "`nclosing the engine`n"

function Close-QlikSenseEngineConnection {
param (
[Parameter(Mandatory=$true)]
[System.Net.WebSockets.ClientWebSocket]$WebSocket
)

$closeTask = $WebSocket.CloseOutputAsync([System.Net.WebSockets.WebSocketCloseStatus]::NormalClosure, "Connection closed", [System.Threading.CancellationToken]::None)
$closeTask.Wait()

return $closeTask
}

Close-QlikSenseEngineConnection -WebSocket $webSocket

Labels (5)
1 Reply
Øystein_Kolsrud
Employee
Employee

There's a lot of code there, and I haven't looked at it all, but it looks to me like you are using a very low level technique for doing websocket communication. I would recommend that you look into utilizing the Qlik Sense .NET SDK for this instead. That should provide you with a much more convenient interface.

The .NET SDK is available for download as a NuGet package:

https://www.nuget.org/packages/QlikSense.NetSDK/

You'll find the reference documentation here:

https://help.qlik.com/en-US/sense-developer/November2023/Subsystems/NetSDKAPIref/Content/Home.htm