1

* Current situation:

I have an app that will take a picture, this picture becomes base64 string. So I have a something like this:

 data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAzwAAAO9CAYAAABHGmKTAAAACXBIWXMAAA9hAA

I Have the following code, that actually works with the link of an image.

var picture = 'http://tonsofcats.com/wp-content/uploads/2013/08/d6040e929550e65fcf1aacbbf0f58e5f-550x411.jpg';

client.messages.create({ 
    to: "+6548554654645", 
    from: "+14654561878", 
    body: "Testing - Testing - Testing - Testing", 
    mediaUrl: picture 
}, function(err, message) { 
    console.log(message.sid);  
});

All of this code, works perfectly. But now I need to send an image that is a base64 string.

Is there a way I can send the image? Like decode it, or save it in cache to be able to send it?

When I try to send the encoded base64 string instead of the URL, I get this error.

enter image description here

Decided to change up a bit the code to see what error I am getting: Found the following:

enter image description here

So, I see that it's a JSON problem, when is trying to send a huge string encoded image

UPDATE

When I console.log(message). This is the message that I get: enter image description here

Monica
  • 1,524
  • 4
  • 27
  • 44
  • 1
    Possible duplicate of [How to convert image into base64 string using javascript](http://stackoverflow.com/questions/6150289/how-to-convert-image-into-base64-string-using-javascript) – ThisClark Apr 06 '17 at 19:52
  • If the rendered page is just doing something like `` It will still work fine. You can use the base64 string as an img src. – Brian Glaz Apr 06 '17 at 19:54
  • You should be able to just provide the base64 string in place of the URL. – TheJim01 Apr 06 '17 at 19:55
  • @TheJim01 I keep getting and error when I send the string as it is. – Monica Apr 06 '17 at 20:08
  • @Monica What error are you receiving? – TheJim01 Apr 06 '17 at 20:12
  • 1
    Maybe I am mistaken, but... an SMS is limited to 140 characters, isn't it? – Psi Apr 06 '17 at 20:18
  • @Psi 160. Twitter introduced the 140 limit because it reserved 20 characters for its use. – TheJim01 Apr 06 '17 at 20:21
  • Right, thanks, but anyway hard to believe that a data url will fit into this little space (unless using multi-sms which is likely not to be applicable here) – Psi Apr 06 '17 at 20:21
  • @TheJim01 The error seems to be coming from Twilio. But anyway. I just want to send the data URL. – Monica Apr 07 '17 at 12:09
  • @Monica Twilio evangelist here. What is the error being returned by Twilio? As long as the URL you are providing as the value of MediaUrl is publicly accessible, what you have looks correct. – Devin Rader Apr 07 '17 at 12:47
  • @Monica I re-read your question, and I'm not sure exactly what you already tried. For all intents and purposes, a dataURL _is_ a URL (and in this case a URL to an image). So if you put your dataURL in place of the image URL in your example (`var picture = 'data:image/png;base64,iVB...';`), it should "just work." – TheJim01 Apr 07 '17 at 13:01
  • @TheJim01 Yeah I already tried that, but gives me an error. I guess the encoded string is too big to send it by sms. I have to figure out something else. But right now I suspect this is the problem – Monica Apr 07 '17 at 13:36
  • @Monica As Devin mentioned, can you please post the error you're receiving? That will help us pin it down. – TheJim01 Apr 07 '17 at 13:39
  • @DevinRader I updated the question with the error that I am getting. Is not very useful I think. – Monica Apr 07 '17 at 13:45
  • @TheJim01 I updated the question with the error that I am getting. Is not very useful I think. – Monica Apr 07 '17 at 13:46
  • @Monica Your error is telling you where the problem lies. Look in `send-sms.js`, line 85. It's trying to access property "`sid`" of an object which is `undefined`. Figuring out why that object is undefined will get you to the next step of solving this problem. – TheJim01 Apr 07 '17 at 14:13
  • @TheJim01 I tried debugging this, but at the end I just decided to print the error instead of the message and I got a different error. I think the problem is JSON or how I am sending the encoded string – Monica Apr 07 '17 at 15:10
  • @Monica Is that second screenshot the result of `console.log(err)`? Can you also log out the `message` parameter? – Devin Rader Apr 07 '17 at 16:15
  • @DevinRader. Yes that is the error from console.log(err). And the first one is from trying to console.log "message.sid" – Monica Apr 07 '17 at 16:20
  • @Monica can you `console.log(message)` – Devin Rader Apr 07 '17 at 16:26
  • @DevinRader It displays undefined. Are you sure I can send the entire encoded base64 string? When I send the http url with the image it works, but when I encode it, it just doesn't work. And Twilio has no more documentation on how to do this. – Monica Apr 07 '17 at 16:34
  • 1
    `mediaUrl` only accepts a publicly accessible URL as a value. You can't send an encoded image in that parameter. If you are setting `picture` to some value other than a URL (as you've shown in your post above) that won't work and is probably what Twilio is complaining about. – Devin Rader Apr 07 '17 at 17:06

1 Answers1

0

Twilio doesn't have documentation on this. So the only thing you can do in this situation is decode the base64 String and save it or cache it so that you can have an actual URL to be able to send via SMS. mediaUrl only accepts http or https urls.

Monica
  • 1,524
  • 4
  • 27
  • 44