Jose's Read Only Forum 2023

General Category => General Discussion => Topic started by: Douglas Martin on July 01, 2016, 05:51:09 PM

Title: Rest API Call returns old repsonse
Post by: Douglas Martin on July 01, 2016, 05:51:09 PM
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


Title: Re: Rest API Call returns old repsonse
Post by: José Roca on July 01, 2016, 06:49:20 PM
Looks like it is returning a cached result. Try closing and reopening the connection.
Title: Re: Rest API Call returns old repsonse
Post by: Douglas Martin on July 01, 2016, 07:06:58 PM
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"
Title: Re: Rest API Call returns old repsonse
Post by: José Roca on July 01, 2016, 08:48:20 PM
Don't know. Try

objHTTP.SetRequestHeader("cache-control", "no-cache")
Title: Re: Rest API Call returns old repsonse
Post by: Douglas Martin on July 01, 2016, 09:54:51 PM
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


Title: Re: Rest API Call returns old repsonse
Post by: Douglas Martin on July 06, 2016, 03:31:55 PM
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)
Title: Re: Rest API Call returns old repsonse
Post by: Douglas Martin on July 14, 2016, 04:44:08 PM
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();
Title: Re: Rest API Call returns old repsonse
Post by: Douglas Martin on July 15, 2016, 07:50:59 PM
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
Title: Re: Rest API Call returns old repsonse
Post by: José Roca on July 16, 2016, 01:41:17 PM
Thanks for posting it. May be useful for other users with the same problem.