1
  • I want to send my file with metadata (JSON) in single POST.
  • I know how to do it python.
  • The question is: How to do it in NodeJS?

Thank you!

Python Code:

image_metadata = {'datalocation': 'is001', 'metadata': 'metadata'}
data = {'name': 'mydata.zip', 'data': json.dumps(image_metadata)}
files = {'file': ('mydata.zip', open('./mydata.zip', 'rb'), 'application/zip', {'Expires': '0'})}
print('Sending data')
r = requests.post(url, files=files, data=data)
0

1 Answer 1

1

If you are wanting to send REST requests using Node, you can use the built-in http library..

Although, I find it easier to use axios..

Furthermore, since you are wanting to send file data - this isn't as simple as just sending a POST request, you have to read the file first..

Something like this should work - it sends the .zip file as a Buffer.

const axios = require('axios');
const fs = require('fs');
const path = require('path');

const file = path.resolve(__dirname, './testfile.zip');
const fileData = fs.readFileSync(file);

const postOptions = {
    data: fileData,
    meta: {meta:'data'}
}

let url = "http:/some.url";
axios.post(url, postOptions)
    .then(r => console.log(r))
    .catch(e => console.log(e))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.