If running @Headers()
like this...
import { Get, Request, Headers } from '@nestjs/common';
@Get()
findAll(@Headers() headers: Headers) {
console.log(headers);
}
it gives you
{
"host": "localhost:3001",
"connection": "keep-alive",
"sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"100\"",
"accept": "application/json, text/plain, */*",
"sec-ch-ua-mobile": "?0",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) OtherShtuff",
"sec-ch-ua-platform": "\"Windows\"",
"origin": "http://localhost:3000",
"sec-fetch-site": "same-site",
"sec-fetch-mode": "cors",
"sec-fetch-dest": "empty",
"referer": "http://localhost:3000/",
"accept-encoding": "gzip, deflate, br",
"accept-language": "en-US,en;q=0.9,fr;q=0.8,es;q=0.7,de;q=0.6",
"cookie": "_ga=GA1blahblahblah"
}
so to get the actual host origin as asked, using the below works. However, using @Headers() headers: Header
then calling headers.origin causes typescript errors since Headers doesn't define all the parameters above. So you'd have to define it yourself, so I would stick to one of the two options below.
import { Get, Headers } from '@nestjs/common';
@Get()
findAgain(@Headers('origin') origin: string) {
console.log(origin);
}
// or
@Get()
findEvenMore(@Request() req: Request) {
console.log(req.get("origin"));
console.log(req.headers.origin);
}