TM1 REST API - close the active session

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

TM1 REST API - close the active session

Post by Wim Gielis » Thu May 27, 2021 9:01 pm

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

Excel Most Valuable Professional, 2011-2014
https://www.wimgielis.com ==> 117 TM1 articles and a lot of custom code
Newest blog article: Avoid circular references https://github.com/wimgielis

User avatar
Mike Cowie
Site Admin
Posts: 464
Joined: Sun May 11, 2008 7:07 pm
OLAP Product: TM1, MSAS
Version: Anything thru 11.x
Excel Version: 2003 - 2016
Location: Alabama, USA
Contact:

Re: TM1 REST API - close the active session

Post by Mike Cowie » Tue Jun 01, 2021 3:37 pm

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

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

Re: TM1 REST API - close the active session

Post by Wim Gielis » Tue Jun 01, 2021 8:24 pm

Hi Mike,

Thanks ! In fact, the code stops with an error:
12.png
12.png (36.86 KiB) Viewed 1079 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

Excel Most Valuable Professional, 2011-2014
https://www.wimgielis.com ==> 117 TM1 articles and a lot of custom code
Newest blog article: Avoid circular references https://github.com/wimgielis

Post Reply