[英]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.