【问题标题】:nest.js @Post setting the content-type of the responsenest.js @Post 设置响应的内容类型
【发布时间】:2019-03-03 01:21:54
【问题描述】:

我正在尝试为我的 API 实现一个 POST 端点,它在被调用时会返回一个 HTML 字符串。

我的代码现在是这样的:

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

如何告诉 POST 端点发送正确的内容类型及其响应?我搜索了所有文档,但找不到任何对我有用的东西。

提前感谢您的帮助

【问题讨论】:

    标签: typescript api post content-type nestjs


    【解决方案1】:

    我自己找到了答案。我刚刚在@Post 装饰器后面添加了@Header 装饰器:

    import { Header } from '@nestjs/common'
    
    @Post('/endpoint')
    @Header('content-type', 'text/html')
    public create(): string {
      //
    }
    

    【讨论】:

      猜你喜欢
      • 2015-03-10
      • 2012-04-20
      • 1970-01-01
      • 2010-12-12
      • 2021-05-23
      • 2016-12-05
      • 1970-01-01
      • 2011-03-12
      • 2021-04-20
      相关资源
      最近更新 更多