JavaScript - How to send PDF file to POST /now/attachment/upload

Anthony Calise
Kilo Explorer

I'm trying to attach a PDF file to our tickets via the /now/attachment/upload API.

Having issues figuring out the correct syntax to do this with JavaScript, I have tried the below solution with axios

This returns a 400 - Bad Request error. I'm looking for a code example of using this API with JavaScript

  const config = { headers: { "Content-Type": "multipart/form-data" } };

      await this._axios.post(
        `/now/attachment/upload`,
        {
          table_name: tableName,
          table_sys_id: sysId,
          uploadFile: './FilePath/test.pdf',
        },
        config
      );
5 REPLIES 5

Kieran Anson
Kilo Patron

Hi,

In postman or curl your request would look like:

find_real_file.png

For Axios, you need to use FormData - Web APIs | MDN (mozilla.org)

var bodyFormData = new FormData();
bodyFormData.append('table_name', 'incident');
bodyFormData.append('table_sys_id', '46e2fee9a9fe19810049b49dee0daf58');
bodyFormData.append('uploadFile', 'file_here');

axios({
  method: "post",
  url: "myurl",
  data: bodyFormData,
  headers: { "Content-Type": "multipart/form-data" },
})

Hi Kieran, thanks for the reply.

 

What are you putting in the 'file_here' section on this line?

bodyFormData.append('uploadFile', 'file_here');

 

Lets say its a file on the file system

I've never done this, I'm only going off of tacit knowledge and my understanding of the APIs.

If you're server side, you can do the file path, if you're client side it would need to be an input field

NaveenAppi
Tera Contributor

Hi Anthony,

 

have you fix this issue, if yes

am facing same issue could you please help me.

 

thank you.