Ruby Examples

All media uploaded via https://upload.wistia.com must be transferred as multipart-form encoded data inside the body of an HTTP-POST. This can be achieved in Ruby quite simply using the 'multipart-post' gem.

Installation:

$ gem install multipart-post

Example Code

# upload_ruby_gem.rb
require 'net/http'
require 'net/http/post/multipart'

def post_video_to_wistia(name, path_to_video)
  uri = URI('https://upload.wistia.com/')

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true

  # Construct the request.
  request = Net::HTTP::Post::Multipart.new uri.request_uri, {
    'access_token' => '<ACCESS_TOKEN>',
    'contact_id'   => '<CONTACT_ID>', # Optional.
    'project_id'   => '<PROJECT_ID>', # Optional.
    'name'         => '<MEDIA_NAME>', # Optional.

    'file' => UploadIO.new(
                File.open(path_to_video),
                'application/octet-stream',
                File.basename(path_to_video)
              )
  }

  # Make it so!
  response = http.request(request)

  return response
end

There's a Gem For That

To make using the Upload API even easier, we created a Ruby gem that lets you use it via the command line!

AJAX Examples

An example of uploading a file from a URL using jQuery. It's advisable to generate an API token from your account with upload-only permissions for this.

<!-- ajax_file_upload.html -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script>
  var access_token = "API token with upload permission";
  var url = "Your video file URL";
  var requestData = jQuery.param({
    url: url
  });

  $.ajax({
    type:'POST',
    url: 'https://upload.wistia.com',
    data: requestData,
    headers: { Authorization: `Bearer ${access_token}` }
    contentType: 'application/x-www-form-urlencoded',
    cache: false,
    processData: false,
    success:function(data) {
      console.log(data);
      alert('Success!');
    },
    error: function(data) {
      console.log(data);
      alert('Error');
    }
  });
</script>