ClickUp API - Attachments

649 views
Skip to first unread message

Sheets Ninja

unread,
Sep 6, 2022, 8:00:08 PM9/6/22
to Google Apps Script Community
So I'm trying to work with ClickUp api, and I got the part figured out to create a new task, but I'm trying to attach a file to the task, and I'm not quite sure how to translate ClickUp's documentation into apps script code.

My current error for the code I have is: "Multipart: Boundary not found"

Here's the documentation for adding an attachment: https://jsapi.apiary.io/apis/clickup20/reference/0/attachments.html

And here is my code, where "taskId" is the task id, and "file" is a blob.

function addAttachment(taskId,file) {

  let bytes = file.getBytes();
  let options = {
    'method': 'POST',
    'muteHttpExceptions': true,
    headers: {
      'Authorization': token,
      'Content-Type': `multipart/form-data`
    },
    'payload': `attachment: ${bytes} (file) filename: attachment.pdf (string)`
  }

  let response = JSON.parse(UrlFetchApp.fetch(url,options).getContentText());
  console.log(response);
}

Any help would be amazing!

Tanaike

unread,
Sep 6, 2022, 8:59:36 PM9/6/22
to Google Apps Script Community
From your provided official document, I understood your question as follows.

You want to convert the following Node.js script to Google Apps Script.

const FormData = require('form-data');
const axios = require('axios');
const fs = require('fs');

const form = new FormData();

form.append('filename', '')
form.append('attachment', fs.createReadStream('./img.png'));

const headers = form.getHeaders();

headers.authorization = `Bearer ${your_api_token}`;

axios({
  method: 'post',
  url: 'https://api.clickup.com/api/v2/task/123/attachment',
  data: form,
  headers,
})
  .then(() => console.log('success'))
  .catch(() => console.log('fail'));


In this case, how about the following modification?

From your showing script, I guessed "file" is Blob. Please be careful about this.

function addAttachment(taskId, file) {
  // let bytes = file.getBytes(); // In this modification. "file" is directly used as a Blob.

  let url = `https://api.clickup.com/api/v2/task/${taskId}/attachment`;
  let options = {
    method: 'POST',
    muteHttpExceptions: true,
    headers: { 'Authorization': token },
    payload: { filename: "sample filename", attachment: file },
  }
  let response = JSON.parse(UrlFetchApp.fetch(url, options).getContentText());
  console.log(response);
}


In the case of "multipart/form-data" with UrlFetchApp, it is not required to include "Content-Type" in the request header. The request body is automatically created by UrlFetchApp.

In this modification, the request of Google Apps Script is the same as that of Node.js. But, if an error occurs, please confirm your variables again. And, if the status code of 403 occurs, I'm worried that the API might not be able to be requested from the Google side.
Reply all
Reply to author
Forward
0 new messages