-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhttpRequest.bas
More file actions
63 lines (52 loc) · 2.33 KB
/
Copy pathhttpRequest.bas
File metadata and controls
63 lines (52 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
'''''''''''''''''''''''''''''''''''''''''''''''
' Http Request '
'''''''''''''''''''''''''''''''''''''''''''''''
'receives url as string, optional request method (or legacy post boolean), and optional request body
'outputs the http response
'this function defaults to a GET request unless a supported request method is provided
Function httpRequest(url As String, Optional post As Variant, Optional requestBody As Variant) As String
'dimension variables
Dim hReq As Object
Dim apiKeys as String: apiKeys = ""
Dim httpType as String: httpType = "GET"
'allow legacy boolean post flag while supporting explicit request methods
If Not IsMissing(post) Then
If VarType(post) = vbBoolean Then
If CBool(post) Then
httpType = "POST"
End If
ElseIf Trim$(CStr(post)) <> "" Then
httpType = UCase$(Trim$(CStr(post)))
End If
End If
Select Case httpType
Case "GET", "HEAD", "POST", "PUT", "DELETE", "CONNECT", "OPTIONS", "TRACE", "PATCH"
Case Else
httpType = "GET"
End Select
'on error return ""
On Error GoTo httpRequestError
'create XML HTTP object
Set hReq = CreateObject("MSXML2.XMLHTTP")
'open request and assign headers
With hReq
.Open httpType, url, False
.SetRequestHeader "Authorization", "Basic " & common.Base64Encode(apiKeys)
If IsMissing(requestBody) Or httpType = "GET" Or httpType = "HEAD" Then
.Send
Else
.Send requestBody
End If
End With
'return response
If httpType = "HEAD" Then
httpRequest = hReq.GetAllResponseHeaders
Else
httpRequest = hReq.ResponseText
End If
'garbage collection
Set hReq = Nothing
Exit Function
httpRequestError:
httpRequest = ""
End Function