Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop caching my HTTP request & response in ionic 3

I want to stop caching my API request and response which the native-http plugin stored its cache and its creating issue with my app.

All-time API works fine but when I get a 404 or 401 error from the server it will cache it in my app and then after all time I will get a timeout error with 1 status.

To overcome this issue I need to uninstall the app and reinstall it again will work as expected.

Any Idea How to stop caching HTTP requests and responses?

Or how to resolve the issue of the timeout with 1 status?

I have tried the below things in my request header but still no success.

self.httpPlugin.setHeader('*', 'authorization', 'Bearer ' + token);
self.httpPlugin.setHeader('*', 'Cache-control', 'no-cache');
self.httpPlugin.setHeader('*', 'Cache-control', 'no-store');
self.httpPlugin.setHeader('*', 'Expires', '0');
self.httpPlugin.setHeader('*', 'Pragma', 'no-cache');

Also added a dummy unique param in my request to make the unique request of my API call like below.

self.httpPlugin.setHeader('*', 'ExtraDate', new Date().toString());

Anyone facing this kind of issue in Ionic 3?

Tried this thread suggestions but no luck at all.

Suggest any solution for this issue.

Edit:

Full Request code:

/**
   * Get Search result from server.
   */
getCaseListBySearchText(searchText: string): Observable<any> {
    let self = this;

  return Observable.create(function(observer) {
    self.getToken().then(token => {
      console.log("Token : ", token);

      // let rand = Math.random();
      self.httpPlugin.setHeader("*", "authorization", "Bearer " + token);
      self.httpPlugin.setHeader("*", "Cache-control", "no-cache");
      self.httpPlugin.setHeader("*", "Cache-control", "no-store");
      // self.httpPlugin.setHeader("*", "Expires", "0");
      self.httpPlugin.setHeader("*", "Cache-control", "max-age=0");
      self.httpPlugin.setHeader("*", "Pragma", "no-cache");
      self.httpPlugin.setHeader("*", "ExtraDate", new Date().toString());

      self.httpPlugin
        .get(self.url + "/CaseList?caseNum=" + searchText, {}, {})
        .then(response => {
          console.log("Response Success : " + JSON.stringify(response));
          let jsonResponse = JSON.parse(response.data);
          console.log("JSON OBJECT RESPONSE : " + jsonResponse);
          observer.next(jsonResponse);
        })
        .catch(error => {
          if (error.status == 403) {
            console.log("Token expired : " + JSON.stringify(error));
            self.isTokenExpired = true;
            //Removing Old Token
            self.storage.remove(Constants.AUTH_DATA);
            observer.error(error);
          } else {
            console.log("Error : " + error);
            console.log("Error " + JSON.stringify(error));
            observer.error(error);
          }
        });
    })
    .catch(error => {
      console.log("Error : " + error);
      observer.error(error);
    });
});


}
like image 682
CodeChanger Avatar asked Oct 08 '18 08:10

CodeChanger


People also ask

How do I disable HTTP caching?

Here's how... When you're in Google Chrome, click on View, then select Developer, then Developer Tools. Alternatively, you can right click on a page in Chrome, then click Inspect. Click on the Network tab, then check the box to Disable cache.

Are HTTP requests cached?

All HTTP requests that the browser makes are first routed to the browser cache to check whether there is a valid cached response that can be used to fulfill the request. If there's a match, the response is read from the cache, which eliminates both the network latency and the data costs that the transfer incurs.


1 Answers

After lots of R&D and web searches regarding the above issue, I found a solution to clean my request cache as above all header not work to clean cache.

In the HTTP Advanced plugin, there is one method to clear my cookies.

clearCookies()

Clear all cookies.

By using the above method in my class constructor to clear cookies before calling any API.

So what it will do clear all my cookies and my issue related to old cookies will be solved in this way.

constructor(
    public storage: Storage,
    public httpPlugin: HTTP,
    private platform: Platform
  ) {
    // enable SSL pinning
    httpPlugin.setSSLCertMode("pinned");
    //Clear old cookies
    httpPlugin.clearCookies();
  }

The above code solves my issue.

Thanks all for your quick guidance and suggestions.

comment on this if this is not the right way to clear my old request data.

like image 138
CodeChanger Avatar answered Sep 21 '22 15:09

CodeChanger