Skip to main content

Using the nodejs package, I'm able to successfully send a MMS message containing an image when I load the image directly from the file system (using fs.createReadStream() like the example in the api docs shows). However, I want to load the image from a url instead. When I try using a Readable stream, the api response is just 400 - Bad Request.

Any ideas what could be wrong?

Here's the code snippet:

const FormData = require('form-data');
let fd = new FormData();

let txt = {
from: { phoneNumber: this.phone },
to: [
{ phoneNumber: to }
],
text: msg
}

fd.append('json', new Buffer.from(JSON.stringify(txt)), {
contentType: 'application/json'
} );

const resp = await axios(url, {method: 'GET', responseType: 'arraybuffer'});

fd.append('attachment', Readable.from(Buffer.from(resp.data, 'binary')), {filename: 'file.png', contentType: resp.headers['content-type']});

const resp = await this.platform.post(endpoint, txt);


I've confirmed the axios download is successful ... not sure what I'm missing with the Readable stream, though.

Why do you need the Readable.from. Just remove it. Also you call the post with the txt part, not the form data!

fd.append('attachment', Buffer.from(resp.data, 'binary'), {filename: 'file.png', contentType: resp.headers['content-type']});

// Fixed
const resp = await this.platform.post(endpoint, fd);



Reply