简体   繁体   English

尝试通过 express js 发布到 API 时出现“不支持的媒体类型”和“内部服务器错误”

[英]getting 'Unsupported Media Type' and 'Internal server error' while trying to post to the API through express js

I have quite strange problem happening.我有一个很奇怪的问题发生。 Basically I am using express server as a proxy to prevent CORS issue happening while trying to use Jira API.基本上我使用快递服务器作为代理,以防止在尝试使用 Jira API 时发生 CORS 问题。

I have created add attachment endpoint which should handle posting multipart/form-data.我创建了添加附件端点,它应该处理发布多部分/表单数据。

The issue is that I am getting问题是我得到

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

in the response.在回应中。 I've found out that whenever I add 'content-type' header the status changes to 500 'internal server error' so both are causing some issues.我发现每当我添加 'content-type' header 时,状态都会更改为 500 'internal server error' 所以两者都会导致一些问题。

Here is my code for this route:这是我这条路线的代码:

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);
  }
});

Try setting 'Content-Type': 'application/json', as a part of header尝试设置'Content-Type': 'application/json',作为 header 的一部分

eg例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试使用Web API POST txt文件,获取415不支持的媒体类型 - Trying to POST txt files using web api, getting 415 Unsupported Media Type POST 得到 415(不支持的媒体类型)而 POSTMAN 返回 200 - POST getting 415 (Unsupported Media Type) while POSTMAN returns 200 尝试进行JSON调用时出现415(不支持的媒体类型)错误 - 415 (Unsupported Media Type) error while trying to make a JSON call 获取错误 415 - 获取 api 中不支持的媒体类型 - Getting error 415 - Unsupported media Type in fetch api 尝试使用 rest api 创建 Hpalm 缺陷时出现“415 Unsupported Media Type” - Getting “415 Unsupported Media Type” when trying to create Hpalm defect using rest api 尝试将formData发布到Spring时的415(不受支持的媒体类型) - 415 (Unsupported Media type) when trying to post formData to Spring 不支持的媒体类型错误 - Unsupported Media type error 从 ASP.NET Core API 端点获取不支持的媒体类型的 HttpCode 415 错误 - Getting HttpCode 415 error of Unsupported media type from ASP.NET Core API endpoint Asp.net core api 从 Javascript 得到“415 Unsupported Media Type”错误,而不是 Postman - Asp.net core api getting “415 Unsupported Media Type” error from Javascript, not Postman 向 Express JS 服务器发出 POST 请求时出错 - Getting error in making POST request to Express JS server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM