Page 1 of 1

TM1 REST API - close the active session

Posted: Thu May 27, 2021 9:01 pm
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 !

Re: TM1 REST API - close the active session

Posted: Tue Jun 01, 2021 3:37 pm
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

Re: TM1 REST API - close the active session

Posted: Tue Jun 01, 2021 8:24 pm
by Wim Gielis
Hi Mike,

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

Re: TM1 REST API - close the active session

Posted: Tue Mar 15, 2022 4:48 am
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.

Re: TM1 REST API - close the active session

Posted: Tue Mar 15, 2022 9:03 am
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.