I have to send this file to an API and testing the request with Curl it works. But trying to replicate the same request in node using many different approaches, it still doesn't work. I get a response saying there is a problem with the file.
Here is the curl request
curl --location --request POST '<api url>' \
--header 'Authorization: Bearer <token>' \
--form 'File=@"C:/Users/aleni/Desktop/Bryce/Truckstop/truckstop_api/bulk_request_VAN.csv"' \
--form 'CalculatedRateFormula="1 Year Avg Rates"' \
--form 'TimeFrameFromDate="2020-06-01"' \
--form 'TimeFrameToDate="2021-05-03"'
Here is my request using axios:
const form = new formData();
form.append("CalculatedRateFormula", "1 Year Avg Rates") // Only one available
form.append("UploadName", 'upload_name_' + mm + '_' + dd + '_' + yyyy)
console.log("Starting CSV load")
form.append("File", fs.createReadStream(__dirname + '/bulk_request_VAN.csv'))
form.append("TimeFrameFromDate", day_before_yyyy + '-' + day_before_mm + '-' + day_before_dd)
form.append("TimeFrameToDate", yesterday_yyyy + '-' + yesterday_mm + '-' + yesterday_dd)
axios.post(global_config.api_url,
form
,
{
headers: {
"Authorization": `Bearer ${token}`,
}
})
.then(response =>{
console.log("Request Submitted")
request_id = response.data.referenceID
console.log(response.data)
if(response.data.data){
retrieveResults(request_id)
}
})
.catch(function (error) {
console.log(error);
});
And here is the stuff I have tried:
- Using the file path instead of fs
- Request with and without __dirname
- fs.readFile and fs.createReadStream
- Wrapping form in brackets inside the axios request.
- Async await
- And all of the possible combitations.
Any ideas where could the problem be? Thanks!