aq2dwe
aq2dwe

Reputation: 45

How to append dynamic values to URL?

I've a Util.ts which will have all the API endpoints.

In my component will import the Api class and make use of the API's.

Now, One of the API endpoint is having dynamic values, that I need to append to the URL.

Utils.ts

export class Api {
    public static PROGRESS_ISSUE = 'https://xxx/rest/api/2/search?jql=project%20%3D%2025764%20AND%20status%20%3D%20%22In%20Progress%22&fields=id,key,status,project&maxResults=100'
}

In the above, I wanted to make the project & status as dynamic values which I'll be passing in the service.

Service.ts

import { Api } from './api/utils'; 

  getJiraOpenIssues(id: number, status:string) {
    return this.http.get<any>( Api.PROGRESS_ISSUE )
    .subscribe(count => console.log(count.total));
  }

Upvotes: 0

Views: 1271

Answers (2)

SiddAjmera
SiddAjmera

Reputation: 39472

You could place some placeholder string on the PROGRESS_ISSUE, and then use replace to replace it with the actual value:

export class Api {
  public static PROGRESS_ISSUE = "https://xxx/rest/api/2/search?jql=project=PROJECTPLACEHOLDER&status=STATUSPLACEHOLDER&fields=id,key,status,project&maxResults=100"
}

Then, in your getJiraOpenIssues method, use replace:

import { Api } from './api/utils';

getJiraOpenIssues(id: number, status: string) {
  const apiUrl = Api.PROGRESS_ISSUE
    .replace('PROJECTPLACEHOLDER', 'YOUR PROJECT ID HERE')
    .replace('STATUSPLACEHOLDER', 'YOUR PROJECT STATUS HERE');
  return this.http.get<any>(apiUrl)
    .subscribe(count => console.log(count.total));
}

Upvotes: 1

Vishw Patel
Vishw Patel

Reputation: 549

You can simply append values like we concat string with + operator.

   getJiraOpenIssues(id: number, status:string) {
        return this.http.get(Api.PROGRESS_ISSUE + '' + id + '/'+status)
            .map((response) => response.json());
    }

Upvotes: 0

Related Questions