Seloka
Seloka

Reputation: 405

passing optional query string parameters to http service call

i need to make an API call which allows me to pass through optional query sting params

 retrieveConts(param1:string,param2?:string):Observable<IContracts> {  
  this.basePath =  "/myConts"; 
      return http.get(this.basePath,{ 
      params: {  
       arg1:param1,
       arg2:param2,
      },
      withCredentials: false
    }) 
     .catch(this._errorHandler); 
  }

With the above code spinet, i have declared param2 as an optional parameter which can or cannot be passed through to the service call, however if param2 gets no value, it then returns undefined for which its understandable.

How do i make the service call to ignore the "arg2" parameter if no value was returned from the param2 variable?

Upvotes: 5

Views: 18123

Answers (6)

You can try something like this:

retrieveConts(param1:string,param2?:string):Observable<IContracts> {  
     var params = {arg1:param1};

     if(param2){
         params = Object.assign(params,{arg2:param2})
     }

     this.basePath =  "/myConts"; 
         return http.get(this.basePath,{ 
         params: params,
         withCredentials: false
     }).catch(this._errorHandler); 
 }

Upvotes: 0

Fernando Raposo
Fernando Raposo

Reputation: 51

I would suggest one minor change: Instead of:

    this.basePath =  "/myConts"; 
    let params = {arg1: param1};
    if (param2) params.arg2 = param2;

        return http.get(this.basePath, { 
            params: params,
            withCredentials: false
        }) 
        .catch(this._errorHandler); 
    }

I would do:

        this.basePath =  "/myConts"; 
    let params = {arg1: param1};
    if (param2) params['arg2'] = param2;

        return http.get(this.basePath, { 
            params: params,
            withCredentials: false
        }) 
        .catch(this._errorHandler); 
    }

By doing this you will avoid compilation error in Angular for instance.

Upvotes: 0

V Djuric
V Djuric

Reputation: 71

You can try something like this:

retrieveConts(p1,p2?): Observable<IContracts> {
    this.basePath =  "/myConts";
    let params = {argument1: p1};
    if (p2)
        params = {argument1: p1, argument2: p2};

    return http.get(this.basePath, {
        params: params,
        withCredentials: false
    })
    .catch(this._errorHandler);
 }

Upvotes: 1

Thomsheer Ahamed
Thomsheer Ahamed

Reputation: 183

instead of passing two values in component, you could pass a object

//Assuming userService is your service // code in your component

userService.retrieveConts({arg1:"val1",arg2:"val2"})

// Code in userService

retrieveConts(param:any):Observable<IContracts> {  
  this.basePath =  "/myConts"; 
      return http.get(this.basePath,{ 
      params: param,
      withCredentials: false
    }) 
     .catch(this._errorHandler); 
  }

or you can also check for param2 and append it to object

retrieveConts(param1:string,param2?:string):Observable<IContracts> {  
    this.basePath =  "/myConts"; 
    let params = {arg1: param1};
    if (param2) params.arg2 = param2;

        return http.get(this.basePath, { 
            params: params,
            withCredentials: false
        }) 
        .catch(this._errorHandler); 
    }

or pass empty val, if param2 is undefined or null

retrieveConts(param1:string,param2?:string):Observable<IContracts> {  
        this.basePath =  "/myConts"; 

            return http.get(this.basePath, { 
                params: {arg1: param1, arg2: param2 || ''},
                withCredentials: false
            }) 
            .catch(this._errorHandler); 
        }

or

  retrieveConts(param1:string,param2?:string=""):Observable<IContracts> {
           this.basePath =  "/myConts"; 

            return http.get(this.basePath, { 
                params: {arg1: param1, arg2: param2},
                withCredentials: false
            }) 
            .catch(this._errorHandler); 
        }

Upvotes: 2

user4676340
user4676340

Reputation:

Switch to an object definition to shorten your code (other solutions will make it longer) :

retrieveConts(params: { [key: string]: value: string }) {
  return http
    .get('/myConts', { 
      params,
      withCredentials: false
    }) 
    .catch(this._errorHandler); 
}

The argument is declared as an object that can has a string key with a string value, and you pass it directly as the params of your URL (properties with the same name don't need to be written twice)

Upvotes: 0

Milos Kovacevic
Milos Kovacevic

Reputation: 891

You can do something like this:

retrieveConts(param1:string,param2?:string):Observable<IContracts> {  
    this.basePath =  "/myConts"; 
    let params = {arg1: param1};
    if (param2)
        params = {arg1: param1, arg2: param2};

    return http.get(this.basePath, { 
        params: params,
        withCredentials: false
    }) 
    .catch(this._errorHandler); 
}

Hope this will be helpful!

Upvotes: 5

Related Questions