1
votes

I am using an existing API call to send a file to our cloud provider via Nodejs. I have seen several different methods of doing this online, but figured I would stick to using "fetch" as most of my other API calls have been using this as well. Presently, I keep getting 500 internal server error and am not sure why? My best conclusion is that I am not sending the file properly or one of my pieces of formdata are not resolving correctly. See the below code:

const fetch = require("node-fetch");
const formData = require("form-data");
const fs = require("fs");
var filePath = "PATH TO MY FILE ON SERVER WITH FILE NAME";

var accessToken = "Bearer <ACCESS TOKEN>;
var url = '<API URL TO CLOUD PROVIDER>';
var headers = {
    'Content-Type': 'multipart/form-data',
    'Accept': 'application/json',
    'Authorization': accessToken
};
const form = new formData();
const buffer = fs.readFileSync(filePath);
const apiName = "MY_FILE_NAME";

form.append("Content-Type", "application/octect-stream");
form.append("file", filePath);

console.log(form);

fetch(url, { method: 'POST', headers: headers, body: form })
    .then(response => response.json())
    .then(data => {
        console.log(data)
    })
    .catch(err => {
        console.log(err)
    });

This my first time attempting something like this so I am next to certain I am missing something. Any help with getting me in the right direction is appreciated.

1
at what line it is giving error? - Ashish Modi
It throws error on the back-end presumably because I am not sending something to the server properly. I do not have a way to look at how the script is sending the request, i.e. logging to show the outbound POST to the back-end. Without that I am guessing on what could be the issue. What would be nice is to have something that would show the actual fetch in logging as it will appear to the back-end server. This way I can tell what is not being sent properly as I do have this working in postman and in curl so it would be an easy comparison. - Seth0080
APIs can be implemented in a variety of different ways. I would posit that it'd be nearly impossible for anyone in the SO community to be able to definitively point to why a specific API endpoint would throw a 500 Internal Server Error without more information or documentation. After exhausting all local debugging options for your request, the next step should generally be to contact whichever team supports the API you're attempting to communicate with for assistance. - esqew
So the I found what the issue is by using an NodeJS express echo server. The problem is that the above script doesn't actually upload the file with the fetch call. I will continue troubleshooting and hopefully post the correct fetch code soon..... - Seth0080

1 Answers

2
votes

So the issue was exactly what I mentioned above. The code was not uploading the file I specified. I finally figured out why and below is the modified code which will grab the file and upload to our cloud service provide:

const fetch = require("node-fetch");
const formData = require("form-data");
const fs = require("fs");
var apiName = process.env['API_PATH'];
var accessToken = "Bearer" +" "+ process.env['BEARER_TOKEN'];
var url = process.env['apiEnv'] +"/" +"archive";
var headers = {
    'Accept': 'application/json',
    'Authorization': accessToken,
};
const form = new formData();
const buffer = fs.readFileSync(apiName);


const uploadAPI = function uploadAPI() {
    form.append("Content-Type", "application/octet-stream");
    form.append('file', buffer);

    fetch(url, {method: 'POST', headers: headers, body: form})
        .then(data => {
            console.log(data)
        })
        .catch(err => {
            console.log(err)
        });
};

uploadAPI();

Being new to Javascript/Nodejs I wasn't really sure what the "buffer" variable did. After finally figuring it out I realized I was adding too many body form params to the request and the file was not being picked up and sent to the provider. All code above is using custom variables, but if for whatever reason someone wants to use it, then simply replace the custom variables with your own....Thanks again for any and all assistance....