Search code examples
angulartypescripthttpresponse

Status of the response - Angular 5


I have created a simple GET on my base.service to get a response if the URL is valid or not.

/** Check connection */
checkConnection(url: string) {
    return this.http.get(url);
}

and I would like to call it with a URL from one of my components and read the status of the response:

checkConnection(protocol, host, port) {
    const url = protocol.toLowerCase().concat("://").concat(host).concat(":").concat(port);
    this.baseService.checkConnection(url)
        .subscribe(
            response => {
                let status = response.status;
            },
            (err) => console.log(err)
        );
}

but I am getting an error on it

Porperty status does not exist on type 'Ojbect'

so it is not able to compile


Solution

  • Your issue regards typescript itself, as response has no clear type in your code.

    Assuming you're using HttpClient, according to docs, the get observable is not clearly typed (https://angular.io/api/common/http/HttpClient#get).

    A simple solution would be to use any:

    .subscribe(
            (response: any) => {
                let status = response.status;
            },