How to prevent XMLHTTP to get client-side cached result?

NAV2009 R2 Classic - I am downloading an XML file using this code:
l_XMLHTTP: Automation 'Microsoft XML, v6.0'.XMLHTTP60
l_XMLDomDoc: Automation 'Microsoft XML, v6.0'.DOMDocument60
l_InStream: InStream

CREATE(l_XMLHTTP);
l_XMLHTTP.open('GET','HTTP://SomeDomain/SimpleRESTService',FALSE,'SomeUser','SomePwd');
l_XMLHTTP.setRequestHeader('Cache-Control','max-age=0');
l_XMLHTTP.send();
l_InStream := l_XMLHTTP.responseStream;
CLEAR(l_XMLHTTP);

CREATE(l_XMLDomDoc);
l_XMLDomDoc.async := FALSE;
IF NOT l_XMLDomDoc.load(l_InStream) THEN
  ERROR('Unable to load XML');

The first time it works fine. The second time I get the exact same cached XML file, and I keep getting the same cached XML until I restart NAV. Then I get a new version of the XML file. I can also open the URL in Internet Explorer, and each time I open the URL in IE it also updates the client-side cache and I get the new version in NAV.
(I can also open the URL in Edge, but this doesn't update the client-side cache used by IE and "NAV")

How can I prevent my code to get a cached version of the XML file?
Regards
Peter

Best Answer

  • pdjpdj Member Posts: 643
    Answer ✓
    Thank you for your reply.
    I have not been able to get a result with POST - it kept giving me an error.

    However; I managed to solve the issue by adding a dummy parameter with a random value:
    l_XMLHTTP.open('GET','HTTP://SomeDomain/SimpleRESTService'+STRSUBSTNO('?x=%1',CREATEGUID),FALSE,'SomeUser','SomePwd');
    

    Not pretty, but it seems to be a common solution to prevent caching o:)
    Regards
    Peter

Answers

  • txerifftxeriff Member Posts: 492
    Hi,

    I´ve made webservice calls using automations in the past and I never had that issue.
    Can I suggest try with POST?

    Also, try it in postman tool, see how it behaves.
    Also try to clear l_InStream variable.
  • pdjpdj Member Posts: 643
    Answer ✓
    Thank you for your reply.
    I have not been able to get a result with POST - it kept giving me an error.

    However; I managed to solve the issue by adding a dummy parameter with a random value:
    l_XMLHTTP.open('GET','HTTP://SomeDomain/SimpleRESTService'+STRSUBSTNO('?x=%1',CREATEGUID),FALSE,'SomeUser','SomePwd');
    

    Not pretty, but it seems to be a common solution to prevent caching o:)
    Regards
    Peter
  • pdjpdj Member Posts: 643
    I'm sorry I had to reject your answer and that it now is "red", like it is a "bad" answer, which is isn't ! :(
    Regards
    Peter
Sign In or Register to comment.