Home > Software engineering >  VBA Azure function: OK in URL address bar, not OK when I call it from VBA
VBA Azure function: OK in URL address bar, not OK when I call it from VBA

Time:11-23

I tried to find an answer to this on stackoverflow to no avail, so I'd like some help:

I was able to type in a URL azure function and get it to display the results through the body object/method.

I have been trying to retrieve a return from the Azure function through VBA but when I do, I get an "Internal Error" message. Even when I copy paste the URL I create from VBA into the address bar, same thing.

The Javascript in the code has "return ("Hello World!");" for testing of a responseText but I can't seem to get VBA to even grab that because of the HTTP 500 Internal Server error.

What is VBA doing during the construction of the URL string that it would be different if I entered it into the address bar itself?

The Azure function is operating as an API and I am simply doing a GET method to call it and bring back a calculation.

Any help would be appreciated. Here is the VBA code that I am focused on:

Dim xmlhttpb As New MSXML2.XMLHTTP60
Dim resString As String
Dim thisURL As String
'example URL
thisURL = "https://someazurefunction.net/api/somefunc" 
thisURL = thisURL & "?name=" & ptc & "assets=" & assets & ""
'I have also constructed it in one line

xmlhttpb.Open "GET", thisURL, False 
xmlhttpb.Send
Debug.Print xmlhttpb.StatusText  'This returns an Internal Server Error
resString = xmlhttpb.responseText
' resString, when printed, is absolutely nothing, not even a "Null" or "Empty" due to the Internal Server error

As a refresher, I can type in the URL and adjust the parameters and it responds in the body successfully. I can't seem to call it from VBA to get a returned value(s).

I AM able to run it locally and from Azure in Visual Studio Code. AND by actually typing in the URL and hitting "enter". BUT constructing a URL in VBA does NOT seem to be successful when executing the GET method and hence, no responseText.

So again: What is the VBA doing that constructs the URL differently that it can't get a responseText from the call? and What do I need to do to correct it?

Thanks!

-ink49

CodePudding user response:

Missing & to separate your querystring parameters:

thisURL = "https://someazurefunction.net/api/somefunc" 
thisURL = thisURL & "?name=" & ptc & "&assets=" & assets 

Should probably also be URL-encoding ptc and assets

How can I URL encode a string in Excel VBA?

  • Related