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:
- Start Excel
- Select: Tools > Macro > Visual Basic Editor
- Select: New Module
- Select: Tools > References
- Select: Microsoft WinHTTP Services, version 5.1
- Copy/paste the code above in the editor
- Adjust the base URL to match your UrbanCode Deploy server
- Click: View > Immediate Window (to view the debug output)
- Click Run > Run Macro > Run
Was this topic helpful?
Document Information
Modified date:
17 June 2018
UID
swg21683131