Skip to content

Instantly share code, notes, and snippets.

@aimtiaz11
Created March 5, 2020 02:10
Show Gist options
  • Save aimtiaz11/d43a8151ff7fffcf2623e66682c76c0e to your computer and use it in GitHub Desktop.
Save aimtiaz11/d43a8151ff7fffcf2623e66682c76c0e to your computer and use it in GitHub Desktop.
Node Axios - HTTP Request Example
var fs = require('fs');
const axios = require('axios');
const https = require('https');
const uuid = require('uuid');
// Read base64 file content
// 5.1 mb file
let fileContent = fs.readFileSync('5mb.txt', 'utf8');
// 1.5 mb file
//let fileContent = fs.readFileSync('1-5mb.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://example.com/documents',
headers: {
'Content-Type': 'application/json',
'X-Client-ID': 'abc',
'X-Client-Secret': 'def',
'X-Source-System': 'NodeApplication',
'X-TrackingID': `${uuid.v1()}`
},
data: {
"title": "Sample File Title",
"primaryFile": {
"fileName": "sample_file_title.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