Quantcast
Channel: Ionic Forum - Latest topics
Viewing all articles
Browse latest Browse all 70433

Request timeout using cordova-plugin-advanced-http

$
0
0

@rubenmiq wrote:

Hi all,
I am using the advanced plugin to make calls to a legacy api that can’t implement a cors policy.
My problem is that even if i set a timeout the request doesn’t seem to respect that number and throws lots of errors of connection timeout after 10000ms and other errors.

Did any one experienced this, and how did you mitigated the problem?

Here is my code

constructor(private http: HttpClient, private nHttp: HTTP, private plt: Platform) { }

  get(url, customParameters?, altmethod?, attempts?:number, customHeaders?) {
    if(this.plt.is('cordova') && !altmethod) {    
      this.nHttp.setRequestTimeout(30);
      
      if(!attempts)
        attempts=0;

      return from(this.nHttp.get(url, customParameters?customParameters:this.defaultGetParams, customHeaders?customHeaders:this.defaultHeaders)).pipe(
        map((response:any) => {          
          if(response.data)
            return JSON.parse(response.data);                    
          else {
            if(response.status && response.status<=0 && attempts<3) {
              this.get(url, customParameters, altmethod, attempts+1);              
            }
            else
              return response;
          }
        })
      );
    }
    else
      return this.http.get(url, {
        headers: new HttpHeaders(customHeaders?customHeaders:this.defaultHeaders),
        params: new HttpParams({fromObject: customParameters?customParameters:this.defaultGetParams})
      }).pipe(map((response:any) => {         
        return response;      
      }));
  }

  post(url, data, customHeaders?, usePlainData?:boolean, requestTimeOut?:number, attempts?:number) {    
    let httpParams = new HttpParams({ fromObject: JSON.parse(JSON.stringify(data)) });    
    if(this.plt.is('cordova'))  {  
      if(usePlainData)
        this.nHttp.setDataSerializer('json');
      else
        this.nHttp.setDataSerializer('urlencoded');
      if(requestTimeOut)
        this.nHttp.setRequestTimeout(requestTimeOut);
      else
        this.nHttp.setRequestTimeout(30);        
        
      if(!attempts)
        attempts=0;
      
      return from(this.nHttp.post(url, data, customHeaders?customHeaders:this.defaultHeaders)).pipe(
        map((response:any) => {                    
          if(response.data)
            return JSON.parse(response.data);          
          else {            
            if(response.status && response.status<=0 && attempts<3) {
              this.post(url, data, customHeaders, usePlainData, requestTimeOut, attempts+1);
            }
            else
              return response;
          }
        })
      );
    }
    else
      return this.http.post(url, usePlainData?data:httpParams, { 
        headers: new HttpHeaders(customHeaders?customHeaders:this.defaultHeaders) 
      }).pipe(map((response:any) => {    
        return response;
      }));
  }

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 70433

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>