Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save muhdkhokhar/42776805b42b973e9b791ffaa4caa504 to your computer and use it in GitHub Desktop.
Save muhdkhokhar/42776805b42b973e9b791ffaa4caa504 to your computer and use it in GitHub Desktop.
' Add the necessary references to your VBA project:
' - Microsoft XML, v6.0
' - Microsoft Scripting Runtime
Public Function PostJsonWithBearerToken(apiUrl As String, jsonPayload As String, bearerToken As String) As String
Dim http As Object
Dim responseText As String
' Create the HTTP object
Set http = CreateObject("MSXML2.ServerXMLHTTP.6.0")
' Open a POST request
http.Open "POST", apiUrl, False
' Set the required headers
http.setRequestHeader "Content-Type", "application/json"
http.setRequestHeader "Authorization", "Bearer " & bearerToken
' Send the request with the JSON payload
http.send jsonPayload
' Get the response text
responseText = http.responseText
' Return the response
PostJsonWithBearerToken = responseText
End Function
' Example usage
Sub TestPostJsonWithBearerToken()
Dim apiUrl As String
Dim jsonPayload As String
Dim bearerToken As String
Dim response As String
apiUrl = "https://your.api.endpoint/endpoint"
jsonPayload = "{""key1"":""value1"",""key2"":""value2""}" ' Example JSON payload
bearerToken = "your_bearer_token"
response = PostJsonWithBearerToken(apiUrl, jsonPayload, bearerToken)
' Display the response
MsgBox "Response: " & response
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment