View Single Post
 
Old 08-17-2025, 04:23 AM
Pf@nne Pf@nne is offline Windows 11 Office 2021
Novice
 
Join Date: Aug 2025
Posts: 1
Pf@nne is on a distinguished road
Default VBA HTTP Post - Problem with JSON Payload

Moin,

I want to send an HTTP post using VBA.
It works quite well when sent WITHOUT a payload.

Code:
Private Function http_post(url As String, payload As String) As String
  Dim req As MSXML2.ServerXMLHTTP60
  Set req = New MSXML2.ServerXMLHTTP60

  With req
    .Open "POST", url, False
    .setRequestHeader "Accept:", "application/json"
    .setRequestHeader "Content-Type:", "application/json"
    .setRequestHeader "X-Api-Key:", "---"
    .send
    http_post = .responseText
  End With
End Function
The HTTP API can also be further specified via a JSON payload.
The working PYTHON code for this looks like this:

Code:
import requests

url = "https://api.bavest.co/v0/quote"

payload = { "isin": "US02079K3059" }

headers = {
"accept": "application/json",
"content-type": "application/json",
"x-api-key": "---"
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)

For this I extended my code as follows:

Code:
Private Function http_post(url As String, payload As String) As String
  Dim req As MSXML2.ServerXMLHTTP60
  Set req = New MSXML2.ServerXMLHTTP60

  payload = "{ ""isin"": ""US02079K3059"" }"

  With req
    .Open "POST", url, False
    .setRequestHeader "Accept:", "application/json"
    .setRequestHeader "Content-Type:", "application/json"
    .setRequestHeader "X-Api-Key:", "---"
    .send payload
    http_post = .responseText
  End With
End Function
For this I extended my code as follows:
Quote:
{"message": "Symbol does not exist.", "status": "ERROR"}
With this String:
Code:
payload = "{ 'isin': 'US02079K3059' }"
The API returns:
Quote:
{"message": "Internal server error"}

Code:
payload = "{ ""isin"": ""US02079K3059"" }"
Seems to be accepted by the API, but cannot fully interpret the content.

Code:
payload = "{ 'isin': 'US02079K3059' }"
It doesn't even seem to be recognized as valid JSON.
What am I doing wrong here?


Greetings
Pf@nne
Reply With Quote