【问题标题】:Content-Type: application/pdf changing to Content-Type: application/octet-streamContent-Type: application/pdf 更改为 Content-Type: application/octet-stream
【发布时间】:2020-10-27 14:01:21
【问题描述】:

我有一个如下的 GET Http 请求

const getData = async (req: Request, res: Response): Promise<void> => {
  try {
    .... some code...
    const response = await createPdfResponse(location);
    res.setHeader("Accept", "application/pdf");
    res.setHeader("Content-Type", "application/pdf");
    res.setHeader(
      "Content-Disposition",
      "inline; filename=fileName.pdf"
    );

    res.send(200, response);
  } catch (error) {
    const errorBody = error.body;
    const statusCode = error.code;
    res.send(statusCode, { errorBody });
  }
};

当我收到响应时,它带有 Content-Type: application/octet-stream。

我需要 Content-Type: application/pdf。

有人可以帮我解决这个问题吗?我会非常感谢你。

【问题讨论】:

  • 当您从浏览器发出此请求时,您的 PDF 是否无法按预期下载/显示?还是它工作正常,唯一的问题是标题不是你所期望的?

标签: node.js api rest restify


【解决方案1】:

我找到了解决方案。这是由重新定位代码引起的:

https://github.com/restify/node-restify/blob/master/lib/response.js#L477

只需使用res.sendRaw(200, response)

【讨论】:

    猜你喜欢
    • 2012-09-11
    • 2012-06-25
    • 2011-01-26
    • 2013-12-28
    • 2017-11-03
    • 1970-01-01
    • 2012-05-07
    相关资源
    最近更新 更多