Created
July 19, 2024 10:32
-
-
Save muhdkhokhar/42776805b42b973e9b791ffaa4caa504 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' 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