简体   繁体   中英

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.

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.

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

eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM