Skip to content

Instantly share code, notes, and snippets.

@aimtiaz11
Created April 3, 2020 01:11
Show Gist options
  • Save aimtiaz11/b64c9b59642d3d3798e41827124df1de to your computer and use it in GitHub Desktop.
Save aimtiaz11/b64c9b59642d3d3798e41827124df1de to your computer and use it in GitHub Desktop.
NodeJS : HTTP request with content from file
const fs = require('fs');
const axios = require('axios'); // need to npm install
const https = require('https'); // need to npm install
const uuid = require('uuid'); // need to npm install
// 5.1 mb file
let fileContent = fs.readFileSync('fp_dc_setup_guide.txt', 'utf8');
console.log(`File length: ${fileContent.length} characters.\nFile Size: ${ (Buffer.byteLength(fileContent, 'utf8') / (1024*1024)) } MB`);
async function makeRequest() {
const config = {
method: 'POST',
url: 'https://my.api.com/api/v1/foo/bar',
headers: {
'Content-Type': 'application/json',
'X-ClientID': 'blah',
'X-ClientSecret': 'blah',
'X-SourceSystem': 'blah',
'X-TrackingID': `${uuid.v1()}`
},
data: {
"title": "SampleFile NB",
"primaryFile": {
"fileName": "sampleFile.pdf",
"fileContent": `${fileContent}`
}
},
httpsAgent: new https.Agent({
rejectUnauthorized: false
})
}
let res = await axios(config)
console.log(`Response: \nStatus: ${res.status} \nHeaders: ${JSON.stringify(res.headers)}`);
}
makeRequest();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment