1. Daniel Mullings
  2. PowerBuilder
  3. Tuesday, 10 October 2023 23:15 PM UTC

I can successfully grab a ping access cookie from our authentication server using the httpclient object, but how do I setup a call to a rest service with that cookie?

My co-worker successfully does this in powershell using the System.Net.Cookie object and setting the attributes, then calls the rest service.

How can I emulate this in PB?

This is what it looks like in powershell:

$apibody = @{
                  Code = "Code"
                  Command = "commanddata"
                   } | ConvertTo-Json

$Cookie = New-Object System.Net.Cookie
$Cookie.Name = "PingAccessClient"
$Cookie.Value = $value | Out-String
$Cookie.Domain = "mydomain.com"
$Cookie.HttpOnly = $true
$Cookie.Secure = $true
$Cookie.Path = "/"
$WebSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$WebSession.Cookies.Add($Cookie)
$props = @{
                  Uri = "https://restcall.mydomain.com/getmyresponse"
                  Headers = $headers
                  ContentType = $ContentType
                  Method = 'POST'
                  WebSession = $WebSession
                  Body = $apibody
                }
                try{
                    $res = Invoke-RestMethod @props
                    $err = ""
                    }
                catch{
                    $err =$_.Exception
                    $res = ""
                    }

Any help would be much appreciated.

Thanks.

Daniel Mullings Accepted Answer Pending Moderation
  1. Wednesday, 11 October 2023 18:18 PM UTC
  2. PowerBuilder
  3. # 1

I was able to get this to work. Thank you Daryl for the request header tip. All of this stuff is new to me.

I'm posting the PowerBuilder code I'm using to grab the ping access cookie, and the call to the API. Genericized, but you get the idea.

Just in case someone needs to cut and paste a code block for this specific use case.

int li_rc
string ls_body, ls_userid, ls_password, ls_apibody
HttpClient lnv_HttpClient
string as_partition, as_command
string ls_responsestatuscode, ls_responsestatustext, ls_all_headers, ls_response_body
string ls_response_body_suffix, ls_response_body_left, ls_response_api
string ls_cookie, ls_content_type, ls_csrfToken
Long ll_remove_end

ls_userid = "Dan"
ls_password = "*****"
ls_partition = "command_prefix"
ls_command = "command"

//Start the ping access cookie retrieval

ls_body = '{"userId":"' +  ls_userid  + '", "password": "'+ ls_password + '"}'

lnv_HttpClient = Create HttpClient
lnv_HttpClient.SetRequestHeader('Content-Type', 'application/json')
li_rc = lnv_HttpClient.SendRequest("POST", "https://pingaccess.com/ping/getPACookie", ls_body)

if li_rc = 1 then
    ls_responsestatuscode = string(lnv_HttpClient.GetResponseStatusCode())
    ls_responsestatustext = lnv_HttpClient.GetResponseStatusText()
    ls_all_headers = lnv_HttpClient.GetResponseHeaders()
    lnv_HttpClient.GetResponseBody(ls_response_body, EncodingUTF8!) 
    ls_response_body = Mid ( ls_response_body, 14 )
    ll_remove_end = Pos(ls_response_body,",")
    ls_response_body_left = Left (ls_response_body,ll_remove_end - 2) 
    ls_response_body_suffix = ls_response_body_left+ ";Path=/; Domain=company.com; Secure; HttpOnly;"

    MessageBox('Debug', 'Response Status Code = ' + ls_responsestatuscode &
        + '~r~nResponse Status Text = ' + ls_responsestatustext &
        + '~r~nHeaders = ~r~n' + ls_all_headers &
        + '~r~nBody = ~r~n' + ls_response_body &
        + '~r~nSuffix = ~r~n' + ls_response_body_suffix)
else
    MessageBox('Debug', 'Something went wrong.~r~nReturn Code = ' + string(li_rc))
end if
//End the ping access cookie retrieval

//Start the api call with ping access cookie
ls_apibody = '{"PartitionCode":"' +  ls_partition  + '", "Command": "'+ ls_command + '"}'

lnv_HttpClient.Clearrequestheaders( )
lnv_HttpClient.SetRequestHeader('Content-Type', 'application/json')
lnv_HttpClient.SetRequestHeader('CSRFToken', 'Tokenbypass')
lnv_HttpClient.SetRequestHeader( "cookie", ls_response_body_suffix)
ls_content_type = lnv_HttpClient.GetRequestHeader( "content-type")
ls_csrfToken = lnv_HttpClient.GetRequestHeader( "CSRFToken")
ls_cookie = lnv_HttpClient.GetRequestHeader( "cookie")

li_rc = lnv_HttpClient.SendRequest("POST", "https://restapi.com/swagger/api/callapi/", ls_apibody)
if li_rc = 1 then
    ls_responsestatuscode = string(lnv_HttpClient.GetResponseStatusCode())
    ls_responsestatustext = lnv_HttpClient.GetResponseStatusText()
    ls_all_headers = lnv_HttpClient.GetResponseHeaders()
    lnv_HttpClient.GetResponseBody(ls_response_api, EncodingUTF8!) 
    
    MessageBox('Debug', 'Response Status Code = ' + ls_responsestatuscode &
        + '~r~nResponse Status Text = ' + ls_responsestatustext &
        + '~r~nHeaders = ~r~n' + ls_all_headers &
        + '~r~nBody = ~r~n' + ls_response_api)
else
    MessageBox('Debug', 'Something went wrong.~r~nReturn Code = ' + string(li_rc))
end if

//End the api call with ping access cookie
destroy lnv_HttpClient

  

Comment
  1. Armeen Mazda @Appeon
  2. Thursday, 12 October 2023 00:59 AM UTC
Thanks for sharing the solution!
  1. Helpful
There are no comments made yet.
Daryl Foster Accepted Answer Pending Moderation
  1. Wednesday, 11 October 2023 03:15 AM UTC
  2. PowerBuilder
  3. # 2

Hi Daniel,

A cookie is just another request header with name=value pairs.  So I think you could just add a request header to your HttpClient code. e.g.

lnv_HttpClient.SetRequestHeader("Cookie", "PingAccessClient=ValueOfTheCookie")

Have you got any Powerbuilder code at the moment to try and call the REST API?

Comment
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.