Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem of data fetching from cache instead of Sever using XMLHTTP for Restful api

Status
Not open for further replies.

Sng1

Programmer
Aug 3, 2021
65
IN
Every time I fetch data , Server response is different for the same url . So I want each time data to be fetched from web and not cache. How is it possible to do ? I have tries setting .setRequestHeader('Cache-Control','no-cache') also but still data is fetched from cache
 
See the discussion in thread184-1813266

I tried to reproduce your problem but couldn't. Even not using setRequestHeader('Cache-Control','no-cache').
If you can provide the URL without revealing an API key or secret I'd also check whether I can reproduce your caching problem.

As there are many layers in caching, let's test if it's your Windows Internet settings that cause the caching:
Code:
Clear
o = Createobject("Microsoft.XMLHTTP")
*o = CreateObject("MSXML2.ServerXMLHTTP.6.0")
For i = 1 To 2
*   o.Open("GET","[URL unfurl="true"]https://www.random.org/integers/?num=10&min=1&max=100&col=5&base=10&format=plain&rnd=new")[/URL]
   o.Open("GET","[URL unfurl="true"]https://www.random.org/integers/?num=10&min=1&max=100&col=5&base=10&format=plain")[/URL]
   o.Send()
   Do While o.readyState<4
      DoEvents
   EndDo
   ? o.responseText
   ? '---'
Endfor

I've tried both Microsoft.XMLHTTP and MSXML2.ServerXMLHTTP.6.0 as atlopes said in the discussion they are based on different libraries WinInet and WinHTTP.
Also the random.org site request URL has an explicit parameter rnd=new for new random numbers that suggests not using it will repeat a cached result. I tried both URL variants and both XMLHTTP request classes and always get different results.

If running that exact same code and you get repeated random numbers, it points out there is a difference between us that's not random.org server behavior, likely our Windows settings, less likely our ISP acting differently. Nowadays ISPs don't force you through their proxy server. But you might also have a proxy in your company.

In case you won't get different results, the variation of the URL will likely help, I'd not make this the first solution, though. It will be good to know where the caching happens to maybe fix that behavior where it should be reconfigured. Parts of a URL that are allowed to differ are anything you append after a "#" sign, i.e. " but caches might disregard everything after # and lookup a cached version of " anyway, no matter what you add. They can't do that when you add to parameters. And it's harmless to invent parameters the server side does not know. If there's already a question mark in the URL like in my case, you could append something like "&xyz=random", so each request URL differs and neither your browser, any CDN, or a proxy could deliver a cached result. If there's no question mark, well, add it at the end, add "?xyz=random" instead of "&xyz=random". The server could defend against something like that and you could get a cached result anyway, but that's not very likely.

There's no one solution working in any case, though URL variations are usually successful. They just also prevent any valid and useful caching.

Chriss
 
Chriss, The code which you have posted was giving different different random numbers in both the cases. I was using Microsoft.XMLHTTP which was giving cache value. I replaced it with MSXML2.ServerXMLHTTP.6.0 - and it worked . Thanks a lot Chriss for your valuable help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top