[rt-users] Need help getting REST API calls to work using WinHttpRequest from VBA

Guadagnino Cristiano guadagnino.cristiano at creval.it
Mon May 4 05:08:53 EDT 2015


Hi,
you will probably find what you need by looking at the source code of my RTChecker project.
Look here: https://www.assembla.com/spaces/rtcheckerv2

Hope this helps
Cris


On 05/01/2015 04:39 PM, Tim Elkin wrote:
Our goal is to create a Ticket in RT using VBA using the WinHttpRequest object.  We understand that there are 2 login pages involved with RT.

So, first we log into the first RT login page using using a "POST" request and passing the username and password using the SetCredentials function of the WinHttpRequest object.  This request appears to return a valid session cookie which we parse to use in our second WinHttpRequest request.

In making our second request we use a "GET" request setting the 2nd RT login username and password using the SetCredentials function and in addition we now are passing the session cookie (that is we send a portion of the session cookie returned from the previous request) info using the SetRequestHeader (as in SetRequestHeader "Cookie", mvarSessionCookie ).  In this second request we are asking for an RT Ticket to be returned using "https://(our url)/REST/1.0/ticket/(a ticket #)/show"<https://(oururl)/REST/1.0/ticket/(aticket#)/show>.  However, we receive the 401 Authorization error instead.

Some sample code would be great.

Below is the code we are using presently to verify that the session cookie we receive from the 1st RT login page is valid by
using a second request to return an existing RT ticket (which is hard coded for testing purposes):

    Private Const cnstBase_URL As String = "https://[our RT url]"

    With WinHttpReq
        'either of the following lines seems to work and return a cookie
        .Open "POST", cnstBase_URL

        .SetRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0"
        .SetRequestHeader "Connection", "keep-alive"

        'user name and password for 1st RT login page/url
    .SetCredentials cnstRequestor, cnstPWD, HTTPREQUEST_SETCREDENTIALS_FOR_SERVER

        On Error Resume Next
        .Send

        If Err.Number = 0 Then
            If .Status = "200" Then
                On Error Resume Next
                output_Cookie = .getResponseHeader("Set-Cookie")
                On Error GoTo 0

                myCookie = Split(output_Cookie, ";")
                If UBound(myCookie) > 0 Then
                    'implicit conversion to string
                    mvarSessionCookie = myCookie(0)
                End If
            Else
                Debug.Print "HTTP " & .Status & " " & .StatusText
            End If
        Else
            Debug.Print "Error " & Err.Number & " " & Err.Source & " " & Err.Description
        End If
        On Error GoTo 0

    End With

    Set WinHttpReq = Nothing

    If Trim(mvarSessionCookie) = "" Then Exit Function

    'perform second request
    Set WinHttpReq = New WinHttp.WinHttpRequest
    With WinHttpReq
        'get ticket data
        Dim TargetURL As String

        'to test cookie, display a ticket
        'hard coded for testing as this works from the Browser which I thought would be a good test
        'to see if the Cookie variable works
        TargetURL = "https://[our RT url]/REST/1.0/ticket/96494/show"

        .Open "GET", TargetURL, False

        .SetRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0"
        .SetRequestHeader "Connection", "keep-alive"

        .SetRequestHeader "Cookie", mvarSessionCookie

        'user name and password for 2nd RT login page/url
    .SetCredentials cnstBasic_Auth_User, cnstBasic_Auth_PWD, HTTPREQUEST_SETCREDENTIALS_FOR_SERVER

        On Error Resume Next
        .Send

        If Err.Number = 0 Then
            If .Status = "200" Then
                Debug.Print
                Debug.Print .ResponseText
                Debug.Print .GetAllResponseHeaders
            Else
                Debug.Print "HTTP " & .Status & " " & .StatusText
            End If
        Else
            Debug.Print "Error " & Err.Number & " " & Err.Source & " " & Err.Description
        End If
        On Error GoTo 0

        Debug.Print .GetAllResponseHeaders

    End With

    Set WinHttpReq = Nothing


Thanks, in advance,
Tim


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.bestpractical.com/pipermail/rt-users/attachments/20150504/84f1cf6b/attachment.htm>


More information about the rt-users mailing list