• Welcome to Jose's Read Only Forum 2023.
 

Rest API Call returns old repsonse

Started by Douglas Martin, July 01, 2016, 05:51:09 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Douglas Martin

The following code is used to Call a rest API.   The web site has a log of all API Calls.  The log shows the calls to GET all tickets for a customer,  then it shows the POST of a new ticket for that customer.  The next call to GET all the tickets which should now include the newly added ticket gets the same response as the first 'GET all tickets', it does not show up in the web site API log .  It is like it never happened.  I have inserted diagnostics to prove that the call is getting executed.

If I run a second copy of the program call to GET all tickets returns all the tickets including the newly added ticket and the call shows up in the web site's API log.

If the program gets closed and relaunched the call to 'GET all tickets' show up in the web site's API log and it returns all the tickets including the newly added ticket.

Why does the second call to 'GET all tickets' keep getting the old response back?  How does the closing and relaunching the program make the call work?   Do I need to close the connection or cause some sort of refresh?


FUNCTION TeamSupportAPI(ByVal Verb As String, ByVal XMLString As String, ByVal URLPath As String) As STRING
   LOCAL objHTTP AS IXMLHTTPRequest
   LOCAL bstrURL AS WSTRING
   LOCAL bstrResponse AS WSTRING
   LOCAL vXML AS Variant
   vXML = XMLString   
   objHTTP = NEWCOM "Msxml2.XMLHTTP.6.0"
   IF ISNOTHING(objHTTP) THEN
     EXIT FUNCTION
   End If
   ' Opens an HTTP connection to an HTTP resource
   bstrURL = "https://app.teamsupport.com/api"
   bstrURL = bstrURL + URLPath
   objHTTP.Open Verb, bstrURL
   ' Sends an HTTP request to the HTTP server
   objHTTP.SetRequestHeader "Authorization", "Basic " + Get_Base64_Encode($orgID & ":" & $apiToken)
   objHTTP.Send(vXML)
   bstrResponse = "x"
   IF objHTTP.Status = 200 THEN ' HTTP_STATUS_OK=200
      bstrResponse = objHTTP.ResponseText + "<>" + STr$(objHTTP.Status) + "<>" + objHTTP.StatusText
   ELSE
      bstrResponse = objHTTP.ResponseText + "<>" + STr$(objHTTP.Status) + "<>" + objHTTP.StatusText
   END If
   FUNCTION = bstrResponse
END Function


Doug

José Roca

Looks like it is returning a cached result. Try closing and reopening the connection.

Douglas Martin

#2
How do I close  the connection?

I tried objHTTP.Close?

How can I Put the 'Close header'  in the request ?

   objHTTP.SetRequestHeader "Connection", "Connection: close"
Doug

José Roca

Don't know. Try

objHTTP.SetRequestHeader("cache-control", "no-cache")

Douglas Martin

I have attached screen shots of the response headers the first time the api called and the second.  The second is missing Cache-Control, Server, Date,  etc


Doug

Douglas Martin

I have used Fiddler to try to determine why the PB program acts differently than the Chrome Rest Client. Fiddler had a link that explained CACHE. http://fiddler2.com/r/?httperf

As a result I have added the following SetRequestHeaders, but it still does not stop the second call with the same URL from getting the Cached response. I will try setting the 'User-Agent" to my program name, see if that helps.

Not sure if I am missing some kind of upper/lower case issue with the SetRequestHeader parameters?

   ' Opens an HTTP connection to an HTTP resource
   bstrURL = "https://app.teamsupport.com/api"
   bstrURL = bstrURL + URLPath '  + nonce
   ? bstrURL
   objHTTP.Open Verb, bstrURL
   ' Sends an HTTP request to the HTTP server
   objHTTP.SetRequestHeader "Pragma", "no-cache"
   objHTTP.SetRequestHeader "Cache-Control", "no-cache, no-store, must-revalidate"
   objHTTP.SetRequestHeader "Expires", "-1"
   objHTTP.SetRequestHeader "Authorization", "Basic " + Get_Base64_Encode($orgID & ":" & $apiToken)
   WriteDebugLogFile("Verb="+Verb+" bstrURL="+bstrURL+" XML="+Variant$(vXML))
   objHTTP.Send(vXML)
Doug

Douglas Martin

I found one solution.  My quick and dirty workaround at a standard Windows client was
- Internet Options
- General
- Browsing history Settings
- Check for newer Versions of stored pages:
tickle "(x) Every time I visit the Webpage"
Now my Msxml2.XMLHTTP.6.0 Object don't use the Cache anymore ...

It has also been suggested to try

IXMLHTTPRequest http = CreateComObject("Msxml2.ServerXMLHTTP");
http.open("GET", "http://www.bankofcanada.ca/stat/fx-xml.xml", False, "", "");
http.setRequestHeader("Cache-Control", "max-age=0");
http.send();
Doug

Douglas Martin

Turns out using Msxml2.ServerXMLHTTP with
objHTTP.SetRequestHeader "Cache-Control", "max-age=0" also worked.

FUNCTION TeamSupportAPI(ByVal Verb As String, ByVal XMLString As String, ByVal URLPath As String) As STRING
   LOCAL objHTTP AS IXMLHTTPRequest
   LOCAL bstrURL AS WSTRING
   LOCAL bstrResponse AS WSTRING
   LOCAL vXML AS Variant
   vXML = XMLString   
   objHTTP = NEWCOM "Msxml2.ServerXMLHTTP.6.0"
   IF ISNOTHING(objHTTP) THEN
     If PTDebugCfgVal(%PTDEBUGPyriTeamSupportApi) Then WriteDebugLogFile("TeamSupportAPI objHTTP is Nothing")
     EXIT FUNCTION
   End If
   ' Opens an HTTP connection to an HTTP resource
   bstrURL = "https://app.teamsupport.com/api"
   bstrURL = bstrURL + URLPath
   objHTTP.Open Verb, bstrURL
   ' Sends an HTTP request to the HTTP server
   If PTDebugCfgVal(%PTDEBUGPyriTeamSupportApi) Then WriteDebugLogFile("Open bstrURL="+bstrURL)   
   objHTTP.SetRequestHeader "Authorization", "Basic " + Get_Base64_Encode($orgID & ":" & $apiToken)
   objHTTP.SetRequestHeader "Cache-Control", "max-age=0"
   If PTDebugCfgVal(%PTDEBUGPyriTeamSupportApi) Then WriteDebugLogFile("Send Verb="+Verb+" bstrURL="+bstrURL+" XML="+Variant$(vXML))
   objHTTP.Send(vXML)
   bstrResponse = "x"
   IF objHTTP.Status = 200 THEN ' HTTP_STATUS_OK=200
      bstrResponse = objHTTP.ResponseText + "<>" + STr$(objHTTP.Status) + "<>" + objHTTP.StatusText
   ELSE
      bstrResponse = objHTTP.ResponseText + "<>" + STr$(objHTTP.Status) + "<>" + objHTTP.StatusText
   END If
   If PTDebugCfgVal(%PTDEBUGPyriTeamSupportApi) Then WriteDebugLogFile("Response=<"+Trim$(bstrResponse)+">")
   FUNCTION = bstrResponse
END Function
Doug

José Roca

Thanks for posting it. May be useful for other users with the same problem.