1

I am learning nest.js and I am having a hard time setting the response headers..

Here is the code snip

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  //@get('users')
  @Get()
  @Header('Content-Type','text/html')
  getHello(): string {
    console.log('log')
    return this.appService.getHello();
  }
} 

I can see the log in the terminal running. When I open the network tab in google chrome to verify the headers, I don't see anything.

network tab

I am following this tutorial:: https://youtu.be/F_oOtaxb0L8?t=1530 on youtube

youtubevideo screenshot

I looked for similar issues on the web and found this :: https://github.com/nestjs/azure-func-http/issues/407

I looked at the documentation and I think I am using everything correctly:: https://docs.nestjs.com/controllers#headers

not sure where I am goofing

Juan Casas
  • 268
  • 2
  • 13

1 Answers1

0

You are actually doing it correct. It's not related with nestjs. It's because of your browser's devtools.

You can confirm it by running in your terminal curl -v localhost:3000 (If you don't use linux idk, just send a request without your browser and check the headers)

You should see Content-Type: text/html; charset=utf-8 in headers.

You can also disable persist logs and start a new incognito tab and test in the browser tools. For the first response, dev tools should show you the Content-Type header. For some reason it does not show it more than once.

For detailed explanation probably this is a good question to check.

Mansur
  • 1,622
  • 14
  • 27