curl snippets

How to perform a file upload (multipart post) with Ruby

Tagged post, multipart, curl, ruby, restclient, upload  Languages ruby

You have at least 5 options:

require 'rest_client' 'http://localhost:3000/foo', fields_hash.merge(:file =>'/path/to/file'))
Open3.popen3('curl  <and your parameters>') do |input, output, error|
# do something

How to find out where a URL redirects with Ruby and Curl

Tagged curl, redirect, curb  Languages ruby
text = %x[curl -L -i ""]
url = text.scan(/Location: ([^\n]+)/).flatten.last

With the Curb wrapper for Curl you could do this:

result = Curl::Easy.perform(link) do |curl| 
  curl.headers["User-Agent"] = "..."
  curl.verbose = true
  curl.follow_location = true
result.last_effective_url # Returns the final destination URL after x redirects...
result.url # Returns the original url which is redirecting...