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?

6 Answers 6

6

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!

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

1 Comment

It would be nice if you add some explanation to your code
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)

1 Comment

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.
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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.