0

I want to retrieve some data from an API using an GET request. The API takes one parameter encoded as JSON in the body of the request. The following code works fine for sending a POST request with some JSON data however if the type of the request is changed to GET the final request being sent contains no body:

Dim strJSONText As String, strURL As String

strURL = "https://<domain>/<some>/<path>"
strJSONText = "{'<parameter>':'<value>'}"

Set objXMLhttp = CreateObject("Msxml2.XMLHTTP")
objXMLhttp.Open "GET", strURL, False
objXMLhttp.setRequestHeader "Accept", "application/json"
objXMLhttp.setRequestHeader "Content-Type", "application/json"
objXMLhttp.send (strJSONText)

Why is strJSONText being ignored whenever the request type is GET? How do I send a GET request with a body using VBA?

arjan
  • 1
  • 2
  • Why have you decided to pass `strJSONText` ByVal instead of the default ByRef. Have you read all the caveats at https://learn.microsoft.com/en-us/previous-versions/windows/desktop/ms763706(v=vs.85). – Lundt Oct 12 '22 at 20:55
  • Eh... There's no such thing as a GET request with a body. See https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET, for example. – Erik A Oct 12 '22 at 21:15
  • You should maybe check this one https://stackoverflow.com/questions/978061/http-get-with-request-body – milo5m Oct 12 '22 at 21:38

0 Answers0