Created
June 3, 2021 17:50
-
-
Save karlgroves/0b03befad2af598a870e0bc8695fe132 to your computer and use it in GitHub Desktop.
Post the content of a local file to Tenon APIV2 using Axios and Node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Obvs you have you `npm install axios` | |
const axios = require('axios'); | |
const fs = require('fs'); | |
const env = 'https://tenon.io/api/v2/'; // NOTE: if you have a private instance of Tenon, you'd change the domain part of this value | |
const username = 'YOUR_EMAIL_GOES_HERE'; | |
const password = 'PASSWORD_GOES_HERE'; | |
const projectID = 'PROJECTID_GOES_HERE'; | |
const sourceFile = './file.html'; // replace with the path to the file you want to send to Tenon | |
const callbackUrl = 'CALLBACK_URL_GOES_HERE'; | |
// This part authenticates you, reads the | |
axios | |
.post(env + 'auth/', { | |
username: username, | |
password: password | |
}) | |
.then(res => { | |
console.log('statusCode:' + res.status); | |
console.log(res.data.access_token); | |
if(res.status === 200) { | |
const headers = { | |
'Content-Type': 'application/json', | |
'Authorization': 'Bearer ' + res.data.access_token | |
}; | |
let content = fs.readFileSync(sourceFile, {encoding: 'utf8'}); | |
console.log(content.length); | |
let body = { | |
'src': JSON.stringify(content), | |
'callbackUrl': callbackUrl, | |
'projectID': projectID | |
}; | |
axios.post(env, body, { | |
headers: headers | |
}).then(res => { | |
console.log('statusCode:' + res.status); | |
console.log(res.data.data.responseID); | |
}).catch(error => { | |
console.error(error); | |
}); | |
} | |
}) | |
.catch(error => { | |
console.error(error); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment