Import API 400 Bad Request no meaningful error message

I’m working on an integration that requires using Import API. I’m testing it with a small csv file (only contains two lines and around 10 columns) locally. I’ve tested with Postman with the csv file and request body, and the data was imported successfully.

Then I tried to do the same API call in the server, with the following lines:

const csvPath = path.join(__dirname, ‘myfile.csv’);

const fileContent = fs.createReadStream(csvPath);

const formData = new FormData();

formData.append(‘importRequest’, requestStr); formData.append(‘files’, fileContent);

const response = await axios.post(`https://api.hubapi.com/crm/v3/imports/\`, formData, {headers: { Authorization: 'Bearer ’ + oauthToken.accessToken, ‘Content-Type’: ‘multipart/form-data’ }});

I’ve confirmed the requestStr is the same as in Postman (in fact, I got the requestStr here first then pasted it in Postman), and the csv file is also the same. However, the request keeps returning a 400 bad request error. Could anyone provide insights here on any potential causes?

Hi @SSun7, I hope that you are well!

Thanks for reaching out to the HubSpot Community!
While looking around the Community, I did find several threads that you might want to check out:
- The solution from @YGhimire on this post “Getting 400 Bad Request”
- The solution from @jjovick on this post “(400) Bad Request”
- The solution from my colleague Jaycee on this post “400 Error on POST to Contacts API”
Also, I’d love to put you in touch with our Top Experts: Hi @Anton, @sylvain_tirreau and @zach_threadint do you have other suggestions to help @SSun7, please?
Have a lovely day and thanks so much in advance for your valuable contributions! :heart:
Bérangère

Hi,

Have you installed the form-data module (npm install form-data) that you are using in your script? When attaching the file, specify the name and type. Try something like that:

const FormData = require('form-data');
const formData = new FormData();

const headers = {
 Authorization: `Bearer ${oauthToken.accessToken}`,
 ...formData.getHeaders()
};

formData.append('files', fs.createReadStream(csvPath), {
 filename: 'myfile.csv',
 contentType: 'text/csv'
});

const response = await axios.post(url, formData, { headers });

Thank you! This works! I think it’s the headers issue.