0

I am required to send a CSV file stored in my computer to another external API. The CSV file contains some data and it is stored in my computer I need to create an API in NodeJS that sends this CSV file to another server using POST method and with the right headers like:

-H 'accept: application/json' -H 'Authorization: Bearer token -H 'Content-Type: multipart/form-data' -F 'csvFile=@New1.csv;type=text/csv'

How can it be achieved? Please help Thanks

const dbConnect = require('../../config/dbConnect')
const mssql = require('mssql/msnodesqlv8')
const DBConstants = require('../utils/constants')
const dbConfig = require('../../config/database')
const converter = require('json-2-csv')
const fs = require('fs')
const axios = require('axios')
const https = require('https')
const formData = require('form-data')

module.exports.getData = async(req, res) => {
    const httpsAgent = new https.Agent({rejectUnauthorized: false}) 
    const form_data = new formData()
    let pool, result, request_config
    var token
    // get data from sp
    try {
        pool = await dbConnect
        result = await pool.request()
            .input(**************)
            .input(************)
            .execute(**********)
    } catch (err) {
        console.log('Error fetching records from store procedure', err)
    }

    // convert fetched data into csv
    converter.json2csv(result.recordsets[0], (err, csv) => {
        if(err) {
            throw err
        } else {
            fs.writeFileSync('DATA.csv', csv)
        }
    })
    form_data.append('csvFile', fs.createReadStream("DATA.csv"))
    // login to external API
    axios.post('/api/v1/user/login', {
        email: 'login_email',
        password: 'login_pass'
        }, { httpsAgent } )
        .then(response => {
            token = response.data.token
            // now create request_config
            request_config = {
                headers: {
                    "accept": "application/json, text/csv",
                     "Authorization": "Bearer " + token,
                     "Content-Type": "multipart/form-data",
                     ...form_data.getHeaders()
                },
                httpsAgent,
                maxContentLength: Infinity,
                maxBodyLength: Infinity,
            }
        })
        .catch(err => {
            console.log(err)
        }) 
// Making the Upload Call       
axios.post('/api/v1/amc/upload/', form_data, request_config)
            .then(response => {
                res.json(response)
            })
            .catch(err => {
                console.log('error in upload', err)
            })
        res.json(result.recordsets[0]) 
    }
  • What have you already tried? There are several libraries (for instance axios, node-fetch, superagent ...) for doing http requests. Pick the one you like most and check the docs ... – derpirscher Aug 26 '21 at 09:54
  • I have used axios in this case – quadir rahman Aug 26 '21 at 10:08
  • Well, then show what you have tried .. – derpirscher Aug 26 '21 at 10:13
  • Hi @derpirscher, I have added the code..Where am I going wrong? Please help – quadir rahman Aug 26 '21 at 10:47
  • First of all, I don't understand, why you need to read the data from the database, then write it to the disk and then read it again from the disk to send it? Why not just send the data you already have in memory. – derpirscher Aug 26 '21 at 10:53
  • Second: You really shouldn't use `writeFileSync` in an async environment because it's blocking the process. – derpirscher Aug 26 '21 at 10:54
  • And third, the problem is probably with `form_data.append("file", fs.createReadStream(file.path));` See this question on how to solve the issue: https://stackoverflow.com/questions/53038900/nodejs-axios-post-file-from-local-server-to-another-server – derpirscher Aug 26 '21 at 10:55
  • Furthermore `converter.json2csv` *seems* to be async. So the callback won't even be called when you try to access that file ... – derpirscher Aug 26 '21 at 11:30
  • I am fetching it from the db, then converting it into csv, because the external API needs to be feeded with a CSV file only – quadir rahman Aug 26 '21 at 12:21
  • Yeah, I get that, but why are you writing it to the disk just to read it from there in the next step? You can just add `csv` returned from the conversion to your formdata without writing it to disk and reading it from there ... – derpirscher Aug 26 '21 at 12:22
  • Wow! Thanks a lot...As advised, rather than writing it to the disk, I am sending the csv directly...this part is solved...but a new error has come up..unable to verify the first certificate..code: 'UNABLE_TO_VERIFY_LEAF_SIGNATURE'...Any clues? – quadir rahman Aug 26 '21 at 14:21
  • https://stackoverflow.com/questions/20082893/unable-to-verify-leaf-signature – derpirscher Aug 26 '21 at 14:23

0 Answers0