HexaCrop
HexaCrop

Reputation: 4263

axios request not working with form data on external api

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,
  }
);

Upvotes: 0

Views: 331

Answers (1)

Marc Stroebel
Marc Stroebel

Reputation: 2357

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)

Upvotes: 1

Related Questions