Solved How to extract data from POST API request

XelaIrodavlas

Registered User.
Local time
Today, 09:04
Joined
Oct 26, 2012
Messages
175
Hello,

Sorry if this is a silly question. I am learning how to write API requests in VBA and have gotten quite far: the request sends and returns a correct response. eg:

<?xml version='1.0' encoding='UTF-8'?><LoginResultData><token>sfbwjvfbjskdfsSomeTokenJhGiiJ.ylH5T5z3yi2kj</token><ttl>3600000</ttl><units>milliseconds</units></LoginResultData>

My question is, how do I interegate that response? I want to take the resulting "token" value and set it to a variable for later use.

This is my code so far:
Code:
Sub GetKronosKey()

Dim myHTTP As New MSXML2.XMLHTTP60
Dim myDom As New MSXML2.DOMDocument60
Dim myXML As String
Dim myServer As String

'Create Body
myXML = "<?xml version='1.0' encoding='UTF-8'?><login_request><credentials><username>MyName</username><password>MyPassword</password><company>MyCompID</company></credentials></login_request>"

'Load body
myDom.loadXML (myXML)

'Enter URL
myServer = "https://secure.workforceready.eu/ta/rest/v1/login"

'Set type of API Request (Get, Post etc)
myHTTP.Open "POST", myServer, False

'Set headers
myHTTP.setRequestHeader "API-Key", "MyCompAPIKey"
myHTTP.setRequestHeader "Accept", "text/xml" '"application/json"
myHTTP.setRequestHeader "Content-Type", "application/xml"

'Send Request
myHTTP.send (myDom.XML)

'Do stuff with response
Debug.Print myHTTP.responseText 'This works great and I can see the token within the immediate window, but it's wrapped in other text
'Extract value of token:
    '#### This part doesnt work, how do???
    'GetKronosKey = myHTTP.token.value

End Sub

Thanks all :)
 
Hi. Since you asked for xml response, you'll have to parse it and process it as xml. Or, maybe you could try using RegEx if you're only after one piece of information.
 
Hi. Since you asked for xml response, you'll have to parse it and process it as xml. Or, maybe you could try using RegEx if you're only after one piece of information.
Thanks theDBguy, do you have any examples/know any resources I can look into? I've been googling a while but I'm afraid I don't even know what to search for :'D
 
May have solved it myself, adding this to the bottom of my current code:
Code:
'Parse response and do stuff: extract token value and set as variable

    'EG how to do this:
    'GetKronosKey = the value of <token>
   
    'Define the variables needed to create our xml document
    Dim xDoc As MSXML2.DOMDocument60
    Dim xNodes As MSXML2.IXMLDOMNodeList
    Dim xNode As MSXML2.IXMLDOMNode
   
    'First, create the document
    Set xDoc = New MSXML2.DOMDocument60
   
        'Load the response into the document:
        xDoc.loadXML (myHTTP.responseText)
       
    'Look at the first child
    Debug.Print xDoc.childNodes.Item(1).baseName          
   
    'Find the nodes that contain the results
    Set xNodes = xDoc.selectNodes("/LoginResultData")    
   
    'Loop through the results
    For Each xNode In xNodes
        'Extract value of token:
        GetKronosKey = xNode.selectSingleNode("token").Text       'need to convert GetKronosKey to a public function first
        'Debug.Print "-----------------------------------"
        'Debug.Print xNode.selectSingleNode("token").Text        'Success!
    Next

I actually don't think the loop is necesary for this particular API call, but it seems like good practice!
 
Last edited:
Glad to hear you got it sorted out.
 

Users who are viewing this thread

Back
Top Bottom