Aman Solanki
Aman Solanki

Reputation: 61

Fetch URI from Post Data through Get Data

Show 1 textfield with 2 buttons - Post, Get. Take a number as input in a text field. On clicking Post, create an array of the numbers from 1 to that number. Post this array at the URL. Display the response from the Post.On clicking Get, fetch data from the URL returned by the Post and display it.

urlPost = 'https://api.myjson.com/bins';
clist: number[] = [];
strData: string = '';
S1: String = '';
ntext: number;
constructor(private netService: NetService) {}

postData() {
    for (var i = 1; i <= this.ntext; i++) {
        this.clist.push(i);
    }
    this.netService.postData(this.urlPost, this.clist)
        .subscribe(postresp => {
            this.strData = JSON.stringify(postresp);
        });
}

getData() {
    this.netService.getData(this.strData.Uri)
        .subscribe(resp => {
            this.strData = JSON.stringify(resp);
        });
}

this line need to be improved.

this.netService.getData(this.strData.Uri)

Upvotes: 1

Views: 452

Answers (1)

Tushar Walzade
Tushar Walzade

Reputation: 3819

As I understand your question, you simply have a problem with parsing a response from your postData(). So, just refer to the following -

postData() {
    for (var i = 1; i <= this.ntext; i++) {
        this.clist.push(i);
    }
    this.netService.postData(this.urlPost, this.clist)
        .subscribe(postresp => {
            this.S1 = postresp['uri'];    // get URL here
        });
}

getData() {
    this.netService.getData(this.S1)      // use it here
        .subscribe(resp => {
            this.strData = JSON.stringify(resp);
        });
}

See it working here.

Upvotes: 1

Related Questions