Skip to content

Instantly share code, notes, and snippets.

@cp2004
Last active November 5, 2021 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cp2004/0495496da63047fb57b7fc33bd5138b5 to your computer and use it in GitHub Desktop.
Save cp2004/0495496da63047fb57b7fc33bd5138b5 to your computer and use it in GitHub Desktop.
import axios from 'axios';
import { readFileSync } from 'fs';
const FormData = require('form-data');
const file = readFileSync('./whistle_v2.gcode', 'utf-8');
let form = new FormData();
form.append("file", file, {
filepath: "whistle_v2.gcode"
});
form.getLength((err, length) => { // This function is redundant
const instance = axios.create({
baseURL: 'http://localhost/api/',
headers: {
'X-Api-Key': '<api-key>',
},
timeout: 10000
});
instance.post('/files/local', form, {
headers: {
...form.getHeaders(),
'Content-Length': form.getLengthSync(),
},
}).then(res => {
console.log(res.data);
}).catch(err => {
console.log(err);
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment