The waitForResponse method doesn't work with XMLHTTP - excel

I have a code in which I declared a variable like that
Dim http As New XMLHTTP60
then in the code, I used this line
http.Open "POST", urlexample, False
http.Send strArg
How can I use the waitForResponse to wait for the server to respond to the request. I tried looping like that after the .Send statement
While http.ReadyState <> 4: DoEvents: Wend
But this works sometimes well and sometimes doesn't return anything.

Looking through object model seems that this method is not available through MSXML2.XMLHTTP60 but is with WinHttp.WinHttpRequest. The latter does not have a ReadyState property however.
You'll need to investigate the default timeout and see if you need SetTimeOuts on WinHttp object to obtain longer times.
Option Explicit
Public Sub testing()
Dim http As winHttp.WinHttpRequest
Set http = New winHttp.WinHttpRequest
With http
'.SetTimeouts ........
.Open "GET", "http://books.toscrape.com/", True
.setRequestHeader "User-Agent", "Mozilla/5.0"
.send
.waitForResponse 1000
End With
Stop
End Sub
N.B. Your request will not be async if the async arg is set to FALSE in the .Open
Interesting reading: https://github.com/VBA-tools/VBA-Web/issues/337
Seems Tim Hall's VBA-Web would be a nice pre-packaged way to go about this.
WinHTTP

Related

HTTP request to sharepoint site

I'm trying to see if a file exists on sharepoint. For this is use below code. If I use "MSXML2.XMLHTTP.6.0" then I get an access denied error.
With "MSXML2.ServerXMLHTTP.6.0" the oHttpRequest.Status returns always 200 even if the file is not there. The URL gets passsed with URLStr and looks something like this:
"https://company.sharepoint.com/sites/abc/def/xyz.xlsx"
If I open the file with other VBA code the first example runs fine (maybe because authentication was done somehow?). Online many posts said to add "s" to "http" -> "https", but it changed nothing.
The idea is that the workbook checks online if it is the latest version and if not informs the user. Maybe there is a better way to do this?
Public Function checkversionmethod2(URLStr As String) As Boolean
Dim oHttpRequest As Object
'Set oHttpRequest = CreateObject("MSXML2.XMLHTTP.6.0") 'authentication issue
Set oHttpRequest = CreateObject("MSXML2.ServerXMLHTTP.6.0") 'always return 200
With oHttpRequest
.Open "HEAD", URLStr, False ', [UserName], [Password]
.setRequestHeader "Cache-Control", "no-cache"
.setRequestHeader "Pragma", "no-cache"
.send
End With
If oHttpRequest.Status = 200 Then
fileexists = 1
Else
fileexists = 3
End If
Set oHttpRequest = Nothing
End Function

Do I need to a new CreateObject("MSXML2.XMLHTTP") for each request or should I store it for the lifetime of my spreadsheet?

When a button is clicked in Excel, I do a GET:
Set hReq = CreateObject("MSXML2.XMLHTTP")
With hReq
.Open "GET", url, False
.Send
End With
This is some code I grabbed from an example.
This sheet will do quite a lot of API related calls. Should I be storing the hReq as a global object and creating it once, or once per button click?
Dim hReq As New MSXML2.XMLHTTP '* Requires tools reference to `Microsoft XML, v3.0`
With hReq
.Open "GET", url, False
.Send
End With
Given remarks about losing reference, may I point out putting New in the Dim statement will create a new object if the old one is lost.
Aside from that you may recycle this object.

WinHttpRequest fails with automation error but XMLHTTP60 works

I've been doing quite a bit of web scraping over the past year and at some point, for reasons I don't remember anymore, I decided to use the Microsoft WinHTTP Services version 5.1 library as my default solution when sending HTTP requests.
I've never had any problems with it and I have achieved anything I ever attempted to do as far as web scraping is concerned.
That is, until i tried the following:
Sub nse()
Dim req As New WinHttpRequest
Dim url As String, requestPayload As String
url = "https://www.niftyindices.com/Backpage.aspx/getHistoricaldatatabletoString"
requestPayload = "{'name':'NIFTY 50','startDate':'01-Feb-2020','endDate':'01-Feb-2020'}"
With req
.Open "POST", url, False
.setRequestHeader "Content-Type", "application/json; charset=UTF-8"
.send requestPayload
Debug.Print .responseText
End With
End Sub
The .send method fails with a
Run-time error -2147012894 (80072ee2) Automation error
Changing to Dim req As New MSXML2.XMLHTTP60 solves the issue completely.
What am I missing here? Could it be website specific somehow? Is there something in the inner workings of these 2 libraries I should know?
Any input would be appreciated.

POST api authentication in vba

I am trying to connect excel (through vba) to the "teamleader" api v1, via post calls.
api docs here
I tried some solutions which I have found, regarding post calls in vba, but seem to be getting nowhere.
This is what I have so far:
Sub test2()
Dim url As String
Dim objhttp As Object
Set objhttp = CreateObject("MSXML2.ServerXMLHTTP")
url = "https://app.teamleader.eu/api/helloWorld.php"
With objhttp
.Open "POST", url, False
.setRequestHeader "api_group", "xxx"
.setRequestHeader "api_secret", "xxx"
.send
Debug.Print .responseText
End With
End Sub
This however gives me this response:
{"status":"failed","reason":"Please set api_group."}
Does anyone have any idea how to get this to work?
Turns out I needed the put all the authentication in the url itself, this question can be closed.

Operation timed out in xmlhttp.Send in VBA

I am fetching data from website. It was working my laptop till last week without any trouble anytime, All of the sudden, it started to give strange behavior(It works in evening and it fail to work during day time. Can anyone please look into below sample code and let me know the reason?
Sub FailedTest2()
Dim xmlhttp As Object
'Set xmlhttp = CreateObject("MSXML2.XMLHTTP")
Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP.6.0") ' I tried this also
URL = "http://www.nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuoteFO.jsp?underlying=HDFCBANK&instrument=FUTSTK&expiry=26DEC2019"
xmlhttp.Open "GET", URL, False
xmlhttp.setRequestHeader "Content-Type", "text/JSON"
xmlhttp.Send
MsgBox xmlhttp.responseText
End Sub

Resources