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
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.
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.
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.
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.
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 {
//
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With