Skip to main content
Question

Sending MMS from an image url

  • June 10, 2023
  • 1 reply
  • 245 views

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.

1 reply

PhongVu
Community Manager
Forum|alt.badge.img
  • Community Manager
  • 2312 replies
  • June 12, 2023

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


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings