How to make HTTP requests in angular and get the body from the object?

Samson Maben

This is the Image of the API request results

Here I am writing a service named FaqService to make HTTP request and calling that service in a component .

getServers(data: string){
    return this.http.get(Staticdata.apiBaseUrl + "/2.2/search/advanced?key="+ Staticdata.key +"&access_token="+ Staticdata.access_token +"&/2.2/search/advanced?order=desc&sort=activity&accepted=True&closed=True&title=" + data + Staticdata.redirectUrl + "&filter="+ Staticdata.filters)
        .map(
        (response: Response) => {
            const items = response.json();
            return items;
        })
    .catch(
        (error: Response) => {
            return Observable.throw(error);
        });
   }
}   

component.ts

  onRecievingResults(value){
    console.log(value);
    this.router.navigate(['results'], {relativeTo: this.route});

    this.faqService.getServers(value)
        .subscribe(
        (item: any[]) => {
            console.log(item);
        },
        (error) => console.log(error)
    ); 
}

Here in this HTML template how do i pass through the API results and get the body.

</div>
<div  class="entryTitle" *ngFor = "let item of items">
<h1>Search results for :{{ item.body }}</h1>
</div>
</div>
eko

{{ item.body }} will refer to this.item.body in your component. So you'll need to create a field:

public item; // <------- add the field
onRecievingResults(value){
    console.log(value);
    this.router.navigate(['results'], {relativeTo: this.route});

    this.faqService.getServers(value)
        .subscribe(
        (item: any[]) => {
            console.log(item);
            this.item = item; // <------- assign it here
        },
        (error) => console.log(error)
    ); 
}

and change your html to:

<h1>Search results for :{{ item?.body }}</h1>

for a truthy check.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Angular, how to make consecutive, dependent http get requests

Angular 13 : how to get http request body object type?

Angular 2 - Get Headers From Http Requests

How to make http requests from COBOL

How to filter HTTP requests based on body before they get to the server?

How to get value of 'body' object from Slim_Http_Request Object

How to make CORS-enabled HTTP requests in Angular 2?

Angular 4 - How to make bulk http syncronous requests

How to make a sequence of http requests in Angular 6 using RxJS

Node.js how to make http get requests in loop

How to make HTTP Requests over WiFi directly from Android Wear?

How to make http requests from api hosted on heroku

How to get only Message Body from a GET HTTP request?

How to make multiple http requests?

How to specify python requests http put body?

How to make a httpclient get request with header, payload and body? Angular API

How to send body in a HTTP Get Request Angular 9

Angular 7 How to get the body (json) of http error in observable

How to correctly subscribe to a service that is making different HTTP get requests? [Angular]

How to get headers from within Exchange object in the body object

How to GET data through HTTP GET requests from Angular to a locally running Spring Boot rest API on port 8080?

How to GET nested [object Object] data from http client in Angular app?

Angular 2 make array/object by recieved json http get

Angular: how to return the first successful response from the list of http requests

How to make http requests using HTTP Intercepters

How to make concurrent GET requests from url pool

Angular - How to delay HTTP Requests

How to get http responses from multiple requests in the order they're recieved

How to make http.get() request to node.js from angular?