Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nest.js @Post setting the content-type of the response

I am trying to implement a POST endpoint to my API, which returns an HTML string when called at.

My code looks like this at the moment:

import { Controller, Post } from '@nestjs/common';
@Controller()
export class MyController {
    @Post('/endpoint')
    public create(): string {
        return `
            <!DOCTYPE html>
            …
            </html>`;
    }
}

How can I tell the POST endpoint to send the correct content-type together with it's response? I've searched all documentations but was not able to find anything helpful for me.

Thank you in advance for your help

like image 859
Kristian Heitkamp Avatar asked Sep 27 '18 07:09

Kristian Heitkamp


People also ask

How do I change my NestJS response code?

As mentioned, the response status code is always 200 by default, except for POST requests which are 201. We can easily change this behavior by adding the @HttpCode(...) decorator at a handler level. Hint Import HttpCode from the @nestjs/common package.

How do I send responses on NestJS?

To set or send a static response status code for a GET request in Nestjs, we can use the @HttpCode() decorator function from the @nestjs/common module before the Controller class method that handles that GET request.

How do I redirect in NestJS?

To set a static redirection for a GET request in Nestjs, we can use the @Redirect() decorator function from the @nestjs/common module and call it just above the Controller class's method that handles that GET request.

How do I get parameters in NestJS?

To get all query parameter values from a GET request, you can use the @Query() decorator function from the @nestjs/common module inside the parameter brackets of the controller's respective method in Nestjs.


1 Answers

I was able to find the answer myself. I just added the @Header decorator just behind the @Post decorator:

import { Header } from '@nestjs/common'

@Post('/endpoint')
@Header('content-type', 'text/html')
public create(): string {
  //
}
like image 167
Kristian Heitkamp Avatar answered Sep 20 '22 03:09

Kristian Heitkamp