IBM Support

How to configure Microsoft Visual Basic For Applications to use the UrbanCode Deploy REST API

Question & Answer


Question

How do you configure the "WinHttp.WinHttpRequest" object to make PUT, POST, DELETE connections to the IBM UrbanCode Deploy REST API without any need to use cookies?

Cause

The UrbanCode Deploy REST API requires the usage of a cookie for all PUT, POST, DELETE calls made by a client that appears to be a browser. UrbanCode Deploy decides if the client is a browser by inspecting the setting of the User-Agent request header. Microsoft Visual Basic for Applications by default configures this header so that it appears to be a browser. To avoid having to use a cookie for all PUT, POST, DELETE calls, you can set the User-Agent request header to an arbitrary, user selected value.

Answer

ANSWER

Use the function calls:

Dim objhttp As New WinHttp.WinHttpRequest

objhttp.Option(WinHttpRequestOption_UserAgentString) = "vba-tool-client"

to overwrite the default User-Agent header sent by Microsoft Visual Basic For Applications, which contains the word Mozilla and is therefore interpreted as a browser by IBM UrbanCode Deploy.

The value: vba-tool-client is of no relevance, you could use any string that is not recognized as the name of a browser.

The following code sample shows how you can create a new Application in IBM UrbanCode Deploy, using the information from the REST API reference documentation.


Sub CreateApplication()
Debug.Print "Start"
Dim objhttp As New WinHttp.WinHttpRequest

BaseURL = _
"https://hostname:8443/cli/application/create"

objhttp.Open "PUT", BaseURL, False

'Set credentials
objhttp.SetCredentials "admin", "admin", _
HTTPREQUEST_SETCREDENTIALS_FOR_SERVER

'We set our own Agent string to avoid being treated like a browser
'This avoids the need to use the cookie UCD_SESSION_KEY
objhttp.Option(WinHttpRequestOption_UserAgentString) = "vba-tool-client"

' Tell client to ignore any SSL certificate errors
objhttp.Option(WinHttpRequestOption_SslErrorIgnoreFlags) = SslErrorFlag_UnknownCA + _
SslErrorFlag_CertWrongUsage + _
SslErrorFlag_CertCNInvalid + _
SslErrorFlag_CertDateInvalid
'Try to create a new Application from a JSON String
objhttp.SetRequestHeader "Content-Type", "application/json"
objhttp.Send ("{""name"": ""My new application"", " & _
" ""description"": ""New application for command example"", " & _
" ""notificationScheme"": ""Default Notification Scheme"", " & _
" ""enforceCompleteSnapshots"": ""false""}")
Debug.Print "Status: "
Debug.Print objhttp.Status
Debug.Print "Response: "
Debug.Print objhttp.ResponseText

Debug.Print "End"
End Sub

The output will be similar to the following:


Start
Status:
200
Response:
{"id":"85cd4e41-e501-4ccc-be2a-7076e39d17ef","securityResourceId":"075ae4a4-d661-4705-bb68-99577a949113","name":"My new application","description":"New application for command example","created":1409327271121,"enforceCompleteSnapshots":false,"active":true,"tags":[],"user":"admin"}
End

To execute the above from Microsoft Excel, proceed as follows:

  1. Start Excel

  2. Select: Tools > Macro > Visual Basic Editor

  3. Select: New Module

  4. Select: Tools > References

  5. Select: Microsoft WinHTTP Services, version 5.1

  6. Copy/paste the code above in the editor

  7. Adjust the base URL to match your UrbanCode Deploy server

  8. Click: View > Immediate Window (to view the debug output)

  9. Click Run > Run Macro > Run

[{"Product":{"code":"SS4GSP","label":"IBM UrbanCode Deploy"},"Business Unit":{"code":"BU053","label":"Cloud & Data Platform"},"Component":"Integrations - 3rd Party","Platform":[{"code":"PF033","label":"Windows"}],"Version":"6.0;6.0.1;6.0.1.1;6.0.1.2;6.0.1.3;6.0.1.4;6.0.1.5;6.1;6.1.0.1;6.1.0.2","Edition":"","Line of Business":{"code":"LOB45","label":"Automation"}}]

Document Information

Modified date:
17 June 2018

UID

swg21683131