Search code examples
getnestjsbasic-authenticationhttpservice

How to use the basic authentication in NestJS?


So I'm trying to do an API call, and I have to use basic authentication.

The codes are like these :

const headersRequest = {
    'Content-Type'  : 'application/json',
    'Authorization' : `Basic ${this.authToken}`
}

let response = this.httpService.get(url, {headers: headersRequest});

The question is the above get() function call correct?

Because the output of :

console.log(response)

is :

Observable { _isScalar: false, _subscribe: [Function (anonymous)] }

and not the response object which I wanted.

I have tried to explore HttpService get, but couldn't find any detailed documentation related to this basic authentication.

PS: I'm a PHP developer and just learned NestJS in the last couple of days.


Solution

  • The easiest way to get the proper result from the observable is to to just convert it to a promise in this case.

    const response = await this.httpService.get(url, {headers: headersRequest}).toPromise()