首页 > 解决方案 > 为什么我对 JIRA api 创建附件的请求返回一个空数组

问题描述

我在向 jira 云请求添加问题附件时遇到问题。它返回 200 但响应是一个空数组。

const formData = new FormData();
for(var i = 0; i < files.length; i++){
    let file = files[i];
    formData.append("file", file.content, file.name);
}

console.log(formData); // see below

let params = {
    method: 'post',
    url: `https://submissive.atlassian.net/rest/api/3/issue/${createKey}/attachments`,
    data: formData.getBuffer(),
    headers: {
        Accept: "application/json",
        'X-Atlassian-Token': 'no-check',
        'Authorization': getAuthHeader(user, apiToken),
        ... formData.getHeaders()
    }
};

console.log('params: ', params);

let response = await axios(params);
console.log(response);
console.log(response.data); // [] empty array

console.log('attachments added');

表单数据:

FormData {
  _overheadLength: 143,
  _valueLength: 2946927,
  _valuesToMeasure: [],
  writable: false,
  readable: true,
  dataSize: 0,
  maxDataSize: 2097152,
  pauseStreams: true,
  _released: false,
  _streams: [
    '----------------------------470840621872458708605830\r\n' +
      'Content-Disposition: form-data; name="file"\r\n' +
      'Content-Type: application/octet-stream\r\n' +
      '\r\n',
    <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 01 2c 01 2c 00 00 ff e1 28 3a 45 78 69 66 00 00 4d 4d 00 2a 00 00 00 08 00 06 01 06 00 03 00 00 00 01 00 02 ... 2946877 more bytes>,
    [Function: bound ]
  ],
  _currentStream: null,
  _insideLoop: false,
  _pendingNext: false,
  _boundary: '--------------------------470840621872458708605830'
}

长度为0有问题吗?我尝试使用和不使用扩展方法添加格式数据,结果相同。我已经确认我可以使用我构建 API 凭据的帐户添加附件。文件名正确:'file'。我唯一的想法可能是我错误地使用了缓冲区,但上面的表单日志使它看起来正确。

标签: node.jsjira-rest-api

解决方案


万一这对任何人都有帮助。我遇到了同样的问题,问题出在调用formData.append(). 第三个参数必须是一个对象。

// get the attachment file

// what I originally had
formData.append("file", attachment.Body) // in my case the attachment.Body is the buffer

// what fixed the issue
const fileInfo = {
    filename: fileName, // e.g. MyDoc.pdf
    contentType: attachment.ContentType, // e.g. application/pdf
    knownLength: attachment.ContentLength, // e.g. 18059464
}

formData.append("file", attachment.Body, fileInfo)

推荐阅读