0

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!

0

1 Answer 1

1

Solved it. Needed to add the form's headers to the request.

...form.getHeaders()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.