This is the postman code. How do I convert to use with VBA. I have been unable to get the body to read as string.
curl --location --request POST 'https://api.web.com' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--header 'CME-Application-Version: CSET' \
--header 'CME-Application-Vendor: CCC' \
--header 'CME-Application-Name: DDD' \
--header 'CME-Request-ID: 20190722' \
--header 'CME-Transact-Time: 23-AUG-2022 09:30' \
--header 'Authorization: Basic AAA' \
--data-raw '{
"payload": {
"legs": [
{
"sideInd": "BUY",
"strategyRatio": 1,
"symbol": "EW4Q2 P4120"
},
{
"sideInd": "BUY",
"strategyRatio": 1,
"symbol": "EW4Q2 C4160"
},
]
}
}
' Thanks!
CodePudding user response:
I modified my API function to handle a Collection of headers, so it will be easier to change and set different headers to suit your needs. I have adapted your cURL postman script into VBA:
Sub Example()
Dim Body As String
Body = "{ ""payload"": { ""legs"": [ { ""sideInd"": ""BUY"", ""strategyRatio"": 1, ""symbol"": ""EW4Q2 P4120"" }, { ""sideInd"": ""BUY"", ""strategyRatio"": 1, ""symbol"": ""EW4Q2 C4160"" }, ] } }"
Dim Headers As New Collection
Headers.Add Array("Accept", "application/json")
Headers.Add Array("Content-Type", "application/json")
Headers.Add Array("CME-Application-Version", "CSET")
Headers.Add Array("CME-Application-Vendor", "CCC")
Headers.Add Array("CME-Application-Name", "DDD")
Headers.Add Array("CME-Request-ID", "20190722")
Headers.Add Array("CME-Transact-Time", "23-AUG-2022 09:30")
Headers.Add Array("Authorization", "Basic AAA")
Dim Response As String
Response = XML_API("https://api.web.com", "POST", Body, Headers)
Debug.Print Response
End Sub
Private Function XML_API( _
ByVal URL As String, _
ByVal Action As String, _
Optional Body As String = "", _
Optional Headers As Collection) As String
Dim http As Object
Set http = CreateObject("MSXML2.XMLHTTP.6.0")
http.Open Action, URL, False
'Headers
Dim Header As Variant
For Each Header In Headers
http.setRequestHeader Header(0), Header(1)
Next
'Send
If Body <> "" Then
http.Send CVar(Body)
Else
http.Send
End If
'Interpret Response
If http.Status = 200 And Action <> "GET" Then
XML_API = "Successfully Sent!"
Else
If http.Status <> 200 And http.responsetext = "" Then
XML_API = http.statustext
Else
XML_API = http.responsetext
End If
End If
End Function
I tested this answer and I was not able to get a response from "https://api.web.com". Make sure the URL you are trying to contact is ready to accept API requests.