(Solved) – A connection with the server could not be established (WinHTTPRequest5.1) Put call in VBA

  • by

I’m running into issues with put call with vba script. I’m getting

a connection with server could not be established.

Dummy URL: https://company-test.host.com/api/approvals/500/approve

Dim args, vURL, vKey, lngTimeout
Set args = WScript.Arguments

  'Set Time-out
lngTimeout = 30000

vURL = args(0)
vKey = args(1)

 'Set Four Parameters for Time Out (Resolve, Connect, Send and Receive)
  Dim http: Set http = CreateObject("WinHttp.WinHttpRequest.5.1")
  http.SetTimeouts lngTimeout, lngTimeout, lngTimeout, lngTimeout
   Dim url: url = vURL
  Dim key: key = vKey

  With http
     Call .Open("PUT", url, False)
     Call .SetRequestHeader("Accept", "application/json")
     Call .SetRequestHeader("API-KEY", key)
     Call .Send("")
 End With

 If Err.Number = 0 then
     If (http.Status = 200) Then
     '   If Left(http.Status, 1) = 2 Then
       'Request succeeded with a HTTP 2xx response, do something...
        response = http.responseText
         'Output error
          response = http.responseText
   End If
    response = "Error " & Err.Number & " " & Err.Source & " " & Err.Description

 End If
  on Error GoTo 0
 set args = Nothing

Leave a Reply

Your email address will not be published. Required fields are marked *