У меня есть запрос на стороне клиента, который отправит данные формы в виде тела на один из моих маршрутов. Вопрос заключается в том, как передать этот файл в аргументы cURL, чтобы разрешить HTTP-запрос POST к внешнему API.
const addAttachment = async () => {
const formData = new FormData();
formData.append("file", fileInput.files[0]);
const result = await axios.post("http://localhost:5000/attachment", formData);
console.log(result);
};
Это мой маршрут внутри express:
app.post("/attachment", multer().single("file"), async (req, res) => {
console.log(req.file);
const args = `-D- -u admin:admin -X POST -H "X-Atlassian-Token: no-check" -F "file=@${req.file.originalName}" http://localhost:8080/rest/api/2/issue/DP-1/attachments`;
exec("curl " + args, (error, stdout, stderr) => {
console.log("stdout: " + stdout);
console.log("stderr: " + stderr);
if (error) {
console.log("exec error: " + error);
}
});
});
Вот что я получаю как тело запроса, полученное из запроса браузера (req.file):
{
fieldname: 'file',
originalname: 'test.txt',
encoding: '7bit',
mimetype: 'text/plain',
buffer: <Buffer 74 65 73 74 73 65 74 65 73 74 73 74 65 74>,
}
Это то, что я сейчас получаю в консоли:
stderr: curl: (26) Failed to open/read local data from file/application
exec error: Error: Command failed: curl -D- -u admin:admin -X POST -H "X-Atlassian-Token: no-check" -F "file=@test.txt" http://localhost:8080/rest/api/2/issue/DP-1/attachments
Вот документация Jira API для линии cURL, поэтому я пытаюсь следовать ей следующим образом:
curl -D- -u admin:admin -X POST -H "X-Atlassian-Token: no-check" -F "file=@myfile.txt" http://myhost/rest/api/2/issue/TEST-123/attachments