【问题标题】:getting 'Unsupported Media Type' and 'Internal server error' while trying to post to the API through express js尝试通过 express js 发布到 API 时出现“不支持的媒体类型”和“内部服务器错误”
【发布时间】:2025-12-18 22:20:05
【问题描述】:

我遇到了一个很奇怪的问题。基本上,我使用 express 服务器作为代理,以防止在尝试使用 Jira API 时发生 CORS 问题。

我已经创建了应该处理发布多部分/表单数据的添加附件端点。

问题是我得到了

status: 415, statusText: 'Unsupported Media Type',

在响应中。我发现每当我添加“内容类型”标头时,状态都会更改为 500“内部服务器错误”,因此两者都会导致一些问题。

这是我这条路线的代码:

app.post("/attachfile", multer().single("file"), async (req, res) => {
  console.log(req.file);
// Req.file =
// {
//  fieldname: 'file',
//  originalname: 'test.txt',
//  encoding: '7bit',
// mimetype: 'text/plain',
//  buffer: <Buffer 31 32 33>,
//  size: 3
// }

  try {
    const response = await fetch(
      "http://localhost:8080/rest/api/latest/issue/DP-1/attachments",
      {
        method: "POST",
        body: req.file,
        headers: {
          Authorization: "Basic xxx",
          "X-Atlassian-Token": "no-check",
        },
      }
    );

    const result = await response.text();
    console.log(response);
    //response while no content-type header: 'status: 415, message: 'Unsupported Media Type'
    //response with content-type header (multipart/form-data): 'status: 500, message: 'FileUploadException: the request was rejected because no multipart boundary was found'

    res.json(result);
  } catch (error) {
    console.log(error);
  }
});

【问题讨论】:

    标签: javascript node.js express multer


    【解决方案1】:

    尝试将'Content-Type': 'application/json', 设置为标题的一部分

    例如

     headers: {
         Authorization: "Basic xxx",
        'Content-Type': 'application/json; charset=utf-8'',
      }
    

    【讨论】:

      最近更新 更多