【问题标题】:Why does my request to the JIRA api to create an attachment return an empty array为什么我对 JIRA api 创建附件的请求返回一个空数组
【发布时间】:2021-01-08 21:18:51
【问题描述】:

我在向 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.js jira-rest-api


    【解决方案1】:

    如果这对任何人都有帮助。我遇到了同样的问题,问题在于对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)
    

    【讨论】:

      猜你喜欢
      • 2021-12-04
      • 1970-01-01
      • 2021-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-12
      • 1970-01-01
      • 2017-07-25
      相关资源
      最近更新 更多