passing optional query string parameters to http service call

11,624

Solution 1

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!

Solution 2

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); 
        }

Solution 3

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);
 }
Share:
11,624
Seloka
Author by

Seloka

Updated on June 04, 2022

Comments

  • Seloka
    Seloka almost 2 years

    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?

  • mooga
    mooga about 5 years
    It would be nice if you add some explanation to your code
  • Abijith Ajayan
    Abijith Ajayan about 4 years
    But from this solution, Am passing more than 7 params, That time some of them are optional, but from this HTTPPARAMS, every time its passing complete params with undefined values.