简体   繁体   中英

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.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM