Search code examples
angulartypescripthttpgethttpclient

Angular HttpClient Module - Trying to get data from a URL


I am creating a project for my own use, using Angular.

I am trying to store the response from this url as a string. It's the string from the "currentPage.content" variable.

I am trying to use the HttpClient Module, but it is not the response I wanted. Instead, I get the following:

{ _isScalar: false, source: {…}, operator: {…} }

Can anybody please help me get the correct response? It would be greatly appreciated.

Alternatively, this url also has the data I need. The goal here would be to store this content as a string value.

Thanks.


Solution

  • Looks like you're displaying the Observable returned by the HttpClient get call. In order to get the data from your Observable, you need to subscribe to it.

    In your service

    getContent(): Observable<any> {
        const url = "api/url/goes/here";
        return this.http.get(url);
    }
    

    Then in your component, where you want to call the API, call getContent and subscribe to the Observable

    ngOnInit() {
        this.getContent().subscribe(data => {
            console.log(data);
        });
    }