我遇到了一个非常奇怪的问题。基本上,在尝试使用Jira API时,我使用express服务器作为代理来防止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);
}
});
尝试将'Content-Type': 'application/json',
设置为标头的一部分
例如
headers: {
Authorization: "Basic xxx",
'Content-Type': 'application/json; charset=utf-8'',
}