passing optional query string parameters to http service call
Asked Answered
O

6

5

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?

Oruro answered 28/5, 2018 at 10:55 Comment(0)
S
5

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!

Smock answered 28/5, 2018 at 11:1 Comment(0)
A
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); 
        }
Acrilan answered 28/5, 2018 at 11:9 Comment(0)
B
1

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);
 }
Bromleigh answered 2/4, 2019 at 9:1 Comment(1)
It would be nice if you add some explanation to your codeSouter
D
0

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)

Doing answered 28/5, 2018 at 11:3 Comment(1)
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.Netsuke
T
0

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.

Tersanctus answered 3/11, 2020 at 20:36 Comment(0)
Z
0

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); 
 }
Zaidazailer answered 4/9, 2021 at 15:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.