Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
See why IDC MarketScape names Qlik a 2025 Leader! Read more
cancel
Showing results for 
Search instead for 
Did you mean: 
rangam_s
Creator II
Creator II

API request

Hi All,

I have come-up with below script, however it is keep loading but nit giving me any results.

Can you please let me know if anything is missing in my script.

Script:

function GetXrfKey() {

  $alphabet = $Null; For ($a=97;$a -le 122;$a++) { $alphabet += ,[char][byte]$a }

  For ($loop=1; $loop -le 16; $loop++) {

    $key += ($alphabet | Get-Random)

  }

  return $key

}

$xrfKey = GetXrfKey


$cert=Get-ChildItem -Path "cert:\CurrentUser\My" | where {$_.Subject -like "*QlikClient*"}

$url="http://127.0.0.1:4747/qrs/app/about?xrefkey=" + $xrfKey


$hrds=@{"x-qlik-xrfkey"=$xrfKey

"X-Qlik-User"='UserDirectory=DESKTOP-A7R270Q;UserId=GIS-11'}


Invoke-RestMethod -Method "get" -Uri $url -CertificateThumbprint $cert.Thumbprint -Headers $hrds



1 Solution

Accepted Solutions
Levi_Turner
Employee
Employee

Hey Rangam,

I am bit confused by some of the choices that were made here. Here's revamped code which is running clean:

function GetXrfKey() {

  $alphabet = $Null; For ($a=97;$a -le 122;$a++) { $alphabet += ,[char][byte]$a }

  For ($loop=1; $loop -le 16; $loop++) {

    $key += ($alphabet | Get-Random)

  }

  return $key

}

$xrfKey = GetXrfKey

$cert=Get-ChildItem -Path "cert:\CurrentUser\My" | where {$_.Subject -like "*QlikClient*"}

$Data = Get-Content C:\ProgramData\Qlik\Sense\Host.cfg

$FQDN = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($($Data)))

$url="https://$($FQDN):4242/qrs/about?xrfkey=" + $xrfKey

$hdrs = @{}

$hdrs.Add("x-qlik-xrfkey", $xrfKey)

$hdrs.Add("X-Qlik-User", "UserDirectory=INTERNAL; UserId=sa_api")

Invoke-RestMethod -Method "get" -Uri $url -CertificateThumbprint $cert.Thumbprint -Headers $hdrs

Some notes:

  • Lines 10-12 are me using a method for scraping the configured hostname from the Host.cfg
  • Line 12, you:
    • Are hitting 4747 which is the QES port, not the QRS port
    • Have a bad path. /qrs/about rather than /qrs/app/about
    • Have misspelled the xrfkey param
  • Lines 13-15
    • I am adding each header specifically so it's clear how to add more as needed.

Hope that helps.

View solution in original post

5 Replies
cpomeren003
Partner - Creator II
Partner - Creator II

I am quite new to JS, but shouldn't this line:


$xrfKey = GetXrfKey


be changed to:


$xrfKey = GetXrfKey();

So that way you call the function?

rangam_s
Creator II
Creator II
Author

Thanks for request, I am getting the expected output under $xrfKey  even without ().


cpomeren003
Partner - Creator II
Partner - Creator II

Well I am not good enough in JS to debug this, but I would change this topic from:
Assumed Answered.

And I would indent the code correctly, so it's easier to read for the people that do know enough JS.

My only other suggestion would be to use the debugger / console.log() and just go through it step by step, but you probably already did that.

Sorry I couldn't help any further!

rangam_s
Creator II
Creator II
Author

No Problem, but I found an answer for this query wherein I am not passing the content type in my invoke request.

Helping hand is always helpful, no need to apologize

Thank you very much for stepping forward and helping me ...

Levi_Turner
Employee
Employee

Hey Rangam,

I am bit confused by some of the choices that were made here. Here's revamped code which is running clean:

function GetXrfKey() {

  $alphabet = $Null; For ($a=97;$a -le 122;$a++) { $alphabet += ,[char][byte]$a }

  For ($loop=1; $loop -le 16; $loop++) {

    $key += ($alphabet | Get-Random)

  }

  return $key

}

$xrfKey = GetXrfKey

$cert=Get-ChildItem -Path "cert:\CurrentUser\My" | where {$_.Subject -like "*QlikClient*"}

$Data = Get-Content C:\ProgramData\Qlik\Sense\Host.cfg

$FQDN = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($($Data)))

$url="https://$($FQDN):4242/qrs/about?xrfkey=" + $xrfKey

$hdrs = @{}

$hdrs.Add("x-qlik-xrfkey", $xrfKey)

$hdrs.Add("X-Qlik-User", "UserDirectory=INTERNAL; UserId=sa_api")

Invoke-RestMethod -Method "get" -Uri $url -CertificateThumbprint $cert.Thumbprint -Headers $hdrs

Some notes:

  • Lines 10-12 are me using a method for scraping the configured hostname from the Host.cfg
  • Line 12, you:
    • Are hitting 4747 which is the QES port, not the QRS port
    • Have a bad path. /qrs/about rather than /qrs/app/about
    • Have misspelled the xrfkey param
  • Lines 13-15
    • I am adding each header specifically so it's clear how to add more as needed.

Hope that helps.