TM1 REST API - close the active session

Post Reply
Wim Gielis
MVP
Posts: 3105
Joined: Mon Dec 29, 2008 6:26 pm
OLAP Product: TM1, Jedox
Version: PAL 2.0.9.18
Excel Version: Microsoft 365
Location: Brussels, Belgium
Contact:

TM1 REST API - close the active session

Post by Wim Gielis »

Hello,

I want to close the active session after I executed a few REST API calls (e.g. get the list of cubes).
I'm almost there but it's not working in VBA.
In Postman the similar statements work fine.
I don't think I should provide a Body (I would not know what Body to provide and in Postman there is no Body either), but the code stops at the Send request and the Body.

Code: Select all

Sub LogOut()

    Dim sResponse             As String

    sURL = "http://ccccccc:xxx/api/v1/" & "ActiveSession/tm1.Close"
    sUser = "admin"
    sPassword = "apple"

    sResponse = PostREST(sURL, sUser, sPassword, "")

End Sub

Function PostREST(sURL As String, sUsername As String, sPassword As String, sPOST_Body As String) As String

    Dim objHTTP               As New MSXML2.XMLHTTP60

    With objHTTP

        .Open "POST", sURL, False, sUsername, sPassword
        
        .setRequestHeader "Content-Type", "application/json"
        .setRequestHeader "Connection", "close"

        .send sPOST_Body

        If .Status <> "201" Then
            MsgBox "Error with query. The URL is" & vbNewLine & vbNewLine & sURL & "The response text is" & vbNewLine & vbNewLine & .responseText
            Set objHTTP = Nothing
            End
        End If

        PostREST = CStr(.responseText)

        .abort

    End With

End Function
Thanks a lot !
Best regards,

Wim Gielis

IBM Champion 2024
Excel Most Valuable Professional, 2011-2014
https://www.wimgielis.com ==> 121 TM1 articles and a lot of custom code
Newest blog article: Deleting elements quickly
User avatar
Mike Cowie
Site Admin
Posts: 482
Joined: Sun May 11, 2008 7:07 pm
OLAP Product: IBM TM1/PA, SSAS, and more
Version: Anything thru 11.x
Excel Version: 2003 - Office 365
Location: Alabama, USA
Contact:

Re: TM1 REST API - close the active session

Post by Mike Cowie »

Hi Wim,

When you say it stops, what do you mean?

If it's hitting your error check code note that the expected success status codes are different (not 201/Created) for logout call options:
  • api/v1/ActiveSession/tm1.Close: 204/No Content
  • api/logout: 200/OK
Regards,
Mike
Mike Cowie
QueBIT Consulting, LLC

Are you lost without Print Reports in Planning Analytics for Excel (PAfE)? Get it back today, for free, with Print Reports for IBM Planning Analytics for Excel!
Wim Gielis
MVP
Posts: 3105
Joined: Mon Dec 29, 2008 6:26 pm
OLAP Product: TM1, Jedox
Version: PAL 2.0.9.18
Excel Version: Microsoft 365
Location: Brussels, Belgium
Contact:

Re: TM1 REST API - close the active session

Post by Wim Gielis »

Hi Mike,

Thanks ! In fact, the code stops with an error:
12.png
12.png (36.86 KiB) Viewed 3241 times
I don't know what to supply in the Body for the Post command (the logout).
Supplying an empty string does not work. Leaving out the sPOST_Body argument gives an error too.
In Postman I have the impression (based on TM1 samples and they work) that we don't have to provide a Body. In VBA though it seems to expect at least something but I could br wrong here.
Best regards,

Wim Gielis

IBM Champion 2024
Excel Most Valuable Professional, 2011-2014
https://www.wimgielis.com ==> 121 TM1 articles and a lot of custom code
Newest blog article: Deleting elements quickly
User avatar
gtonkin
MVP
Posts: 1192
Joined: Thu May 06, 2010 3:03 pm
OLAP Product: TM1
Version: Latest and greatest
Excel Version: Office 365 64-bit
Location: JHB, South Africa
Contact:

Re: TM1 REST API - close the active session

Post by gtonkin »

Hi Wim,

Came across this post and the error recently.

The errors is basically saying that the call has not finished processing yet and the status is not available.
If you change the Asynch flag on the .Open to True it should behave differently, or introduce a slight delay.

HTH if you have not already solved it and forgotten about this.
Wim Gielis
MVP
Posts: 3105
Joined: Mon Dec 29, 2008 6:26 pm
OLAP Product: TM1, Jedox
Version: PAL 2.0.9.18
Excel Version: Microsoft 365
Location: Brussels, Belgium
Contact:

Re: TM1 REST API - close the active session

Post by Wim Gielis »

Hello George,

Thanks ! I had not forgotten about it but for my pet projects with the REST API, I just let the connection timeout or leave it as is.
I will pick it up again and play around a bit more.
Best regards,

Wim Gielis

IBM Champion 2024
Excel Most Valuable Professional, 2011-2014
https://www.wimgielis.com ==> 121 TM1 articles and a lot of custom code
Newest blog article: Deleting elements quickly
Post Reply