Search code examples
rubytwiliogiphy

Invalid-Content-Type error when trying to send GIF through Twilio SMS


I have this bit of code to send an MMS message with a GIF. (using Ruby with Sinatra, hosted on Heroku).

client.messages.create(
  to: to,
  from: phone, 
  body: message,
  media_url: 'http://media.giphy.com/media/zl170rmVMCpEY/giphy.gif'
)

It fails, and Twilio's debug console shows a 12300 invalid content-type error. I'm certain I'm missing something simple here, but I cannot figure out what.


Solution

  • The URL you are using is returns a different type of content based on the Accept header of the request.

    In Chrome a response with a "Content-Type" header of "text/html". Which is surprising given the .gif suffix on the URL.

    Chrome accept headers look like: Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8

    However if I use curl -I http://media.giphy.com/media/zl170rmVMCpEY/giphy.gif I get Content-Type: image/gif

    If you look at the image URL on the HTML page, in Chrome, it is actually: https://i.giphy.com/zl170rmVMCpEY.webp

    webp is an alternative format to gif, I suspect it is served instead of gif if the browser supports it.

    If Twilio supports webp format images you could use that instead.

    Gify also seem to use mp4 format, it looks like they brand as gif, but don't actually serve gif's to clients which can accept HTML or WebP content.