I am using Asynchronous HTTP Request Class in my access project.
The author has only added GET
. Hence I am trying to add POST
functionality to the class.
The following code I added to the Class
Public Sub PostRequest(serviceURL As Variant, Optional apiBody As String)
On Error GoTo Sub_Err
Set m_oXmlHttp = New MSXML2.XMLHTTP60
m_oXmlHttp.Open "POST", serviceURL, True
'this sets the onreadystatechange call back to an instance of this object
'which causes the default method HandleResponse to be called when the ready
'state changes
m_oXmlHttp.onreadystatechange = Me
m_oXmlHttp.send apiBody
'Error Catching
Sub_Exit:
Exit Sub
Sub_Err:
MsgBox Error$
Resume Sub_Exit
End Sub
When calling the above sub, I am suing the below code in my form.
Private WithEvents oAHlogin As clsAsyncHTTP
Private Sub PostLoginData()
Dim apiURL, apiBody As String
apiURL = "myurl"
apiBody = "mybody"
Set oAHlogin = New clsAsyncHTTP
oAHlogin.PostRequest apiURL, apiBody '*This is where the execution stops*.
End Sub
Private Sub oAHlogin_ResponseReady(ByVal ready As Boolean)
If ready Then
Debug.Print oAHlogin.GetReponseText
End If
End Sub
See above line, where I have mentioned This is where execution stops. Any help is appreciated. I am relatively new to programming. Am I calling the sub incorrectly? Is the parenthesis missing?
I was able to execute GET
correctly as shown by author of the class. The POST
which I added, does not work
Edit 1: Reply to Gord Thompson, making this Q as duplicate: The Question # 1463635 is for ASP, I am asking for Access in VBA which are completely different things.
Edit 2: I have added following references in my Access project.
You need to set headers - At a minimum you need to add this line to your class, before you call
.Send
After messing around with the code a bit more I found that when making an asynchronous POST there were a couple of subtle changes required to the code in the answer to the question I previously marked as duplicate. This is the code that ultimately worked for me:
It doesn't matter if you are "post"ing or "GET"ing. There are few things you need to understand before making this happen.
The process flow is as this:
Important: The callback function will be a class and that class needs the hack.
[STEP1: Preparing the callback class]
Create new Class module in your VBA, call it HTTP_HANDLER_CLASS with following code:
[STEP2: modifying/hacking the call-back class]
This will make the OnReadyStateChange() as the default function which is what we wanted to have and so far, you've done the most important part:
[Step 3: sending requests this example uses SOAP action] Assume a web-service "CallingServiceName" is available to you which takes two parameters iEmail, iPassword and returns a string if the login was success or not.
[Result]
oh btw, Microsoft XML v3 is enough for this.
Above example uses SOAP version but you can use any method. Please let me know if this has helped.