Skip to content

Instantly share code, notes, and snippets.

@wwwins
Created June 12, 2023 05:15
Show Gist options
  • Save wwwins/8c0f7499e8df02e6a2dc5bae0288e6db to your computer and use it in GitHub Desktop.
Save wwwins/8c0f7499e8df02e6a2dc5bae0288e6db to your computer and use it in GitHub Desktop.
Uploading an image(binary data) with nodejs
/**
*
* Uploading binary data with nodejs
* axios + fs.createReadStream
*
* */
const fs = require('fs');
const axios = require('axios').default;
const FormData = require('form-data');
async function main(file) {
let data = new FormData()
const buffer = fs.createReadStream(file)
data.append('file', buffer)
const res = await axios.post('http://localhost/predict', data, {
headers: {'Content-Type': 'multipart/form-data',},
})
console.log(res.data)
}
main(...process.argv.slice(2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment