Sahan Amadoruge
11/03/2021, 5:37 PMRoss Coundon
11/03/2021, 5:38 PMSahan Amadoruge
11/04/2021, 11:20 AMSahan Amadoruge
11/04/2021, 11:21 AMvar axios = require('axios');
var FormData = require('form-data');
var fs = require('fs');
var data = new FormData();
data.append('image', fs.createReadStream('/path/to/file'));
data.append('deviceInformation', '{"brand": "nokia"}');
var config = {
method: 'post',
url: '{API URL}',
headers: {
'x-api-key': '{{apiKey}}',
...data.getHeaders()
},
data : data
};
axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
});
I used this code to check my API locallySahan Amadoruge
11/04/2021, 11:22 AMSahan Amadoruge
11/04/2021, 11:23 AMRoss Coundon
11/04/2021, 12:24 PMSahan Amadoruge
11/04/2021, 12:25 PMRoss Coundon
11/04/2021, 12:28 PMSahan Amadoruge
11/04/2021, 12:29 PMSahan Amadoruge
11/04/2021, 12:31 PMS3.getObject({ Bucket, Key }, function (err, data) {
if (err) {
console.error(err.code, "-", err.message);
}
fs.writeFile(`/tmp/${fileName}`, data.Body, function (err) {
if (err) console.log(err.code, "-", err.message);
const data = new FormData();
const readStream = fs.createReadStream(`/tmp/${fileName}`);
data.append("image", readStream);
data.append("deviceInformation", '{"brand": "samsung"}');
const config: any = {
method: "post",
url: apiURL,
headers: {
...data.getHeaders(),
},
data: data,
};
// // console.log(config);
console.log("Gonna send the api call 4", readStream);
axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log("Got error === ", error);
});
});
});
Did this way. But there has to be a better way to do this 😕Ross Coundon
11/04/2021, 12:44 PMconst file = await S3.getObject({ Bucket, Key }).promise();
const formData = new FormData();
formData.append("image", file.Body);
const res = await <http://axios.post|axios.post>('<https://someUrl>', formData, {
headers: formData.getHeaders()
});
Sahan Amadoruge
11/04/2021, 12:45 PMSahan Amadoruge
11/04/2021, 12:57 PMfile.Body
is a Buffer We need to pass a file name when appending it.
formData.append("image", file.Body, { filename: fileName });
Ross Coundon
11/04/2021, 12:57 PM