0

I have a external API request as given below.

enter image description here

Now I need to write this postman API into an axios API call. But I tried to do many alternative things, but nothing seems to work.

The below code explain the current code I tried to do.

const url = `${this._url}/rest/v1.0/files?project_id=${projectId}`;
const response = await Axios.default.post(
  url,
  {
    file: {
      parent_id: +parentId,
      data: file,
    },
  },
  {
    headers: {
      Authorization: `Bearer ${updatedToken}`,
      'Content-Type': 'multipart/form-data',
      'Procore-Company-Id': company.id,
    },
    maxBodyLength: Infinity,
    maxContentLength: Infinity,
  }
);
2
  • try using npmjs.com/package/form-data Commented Apr 12, 2021 at 7:25
  • did that as well, but it also seems not working, I need a syntax on how to upload using formdata Commented Apr 12, 2021 at 7:30

1 Answer 1

1

using form-data

const form = new FormData();
form.append( 'my_file', fs.readFileSync('/foo/bar.jpg') );

// In Node.js environment you need to set boundary in the header field 'Content-Type' by calling method `getHeaders`
const formHeaders = form.getHeaders();

axios.post('http://example.com', form, {
  headers: {
    ...formHeaders,
  },
})
.then(response => response)
.catch(error => error)
Sign up to request clarification or add additional context in comments.

2 Comments

but the issue is i am using multer to get the file, and it does not have a file path as of now, just the entire file with the buffer data
@Jithin append can also be used with a buffer form.append('my_file_from_buffer', <your multer buffer>, { filename: myfile.jpeg, })

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.