Forum: Ruby on Rails Rails: How to post a file via HTTP as multipart/form-data to Facebook?

Eefa96eb766ca4dd85141bc738245cda?d=identicon&s=25 salamis salamis (salamis)
on 2011-08-19 14:57
I would like to post a new photo to a user using `Net::HTTP::multipart`
from a Heroku application to Facebook.

I have the following JSON object:

    {"message"=>"My message",
"image"=>#<ActionDispatch::Http::UploadedFile:0x00000004242490
@original_filename="neEZYMAnBI.jpg",
@content_type="application/octet-stream", @headers="Content-Disposition:
form-data; name=\"image\";
filename=\"/home/user/public/direct/fb_images/neEZYMAnBI.jpg\"\r\nContent-Type:
application/octet-stream\r\n",
@tempfile=#<File:/app/tmp/RackMultipart20110818-1-18qnwtj>>,
"method"=>"post", "access_token"=>"my_access_token", "format"=>"json"}


I tried to use:

    require 'net/http/post/multipart'

    url =
URI.parse('https://graph.facebook.com/me/photos?access_token=...)
    File.open(params[@tempfile]) do |jpg|
      req = Net::HTTP::Post::Multipart.new url.path,
        "file" => UploadIO.new(jpg, "image/jpeg", "image.jpg")
      res = Net::HTTP.start(url.host, url.port) do |http|
        http.request(req)
      end
    end

But:

 1. It is not sending the data.
 2. I cannot find how to send the params[:message] with the file in
order to have a caption for the image.

Can someone suggest a solution?

Thanks
Eefa96eb766ca4dd85141bc738245cda?d=identicon&s=25 salamis salamis (salamis)
on 2011-08-19 14:57
I also found:

http://forrst.com/posts/post_a_photo_to_the_fb_gra...

    class FbPhotoUploader
      require 'net/http/post/multipart' # multipart-post gem
      require 'mime/types' #mime-types gem
      require 'net/https'

      def upload(path, access_token, options={})
        photo = File.open(path)
        params = {:access_token => access_token}
        params.merge!(options)
        url = URI.parse("https://graph.facebook.com/me/photos")

        req = Net::HTTP::Post::Multipart.new
"#{url.path}?#{params.to_query}",
        "file" => UploadIO.new(photo, mime_for_file(photo), photo.path)

        n = Net::HTTP.new(url.host, url.port)
        n.use_ssl = true
        n.verify_mode = OpenSSL::SSL::VERIFY_NONE
        n.start do |http|
          http.request(req)
        end
      end

    private
      def mime_for_file(f)
        m = MIME::Types.type_for(f.path.split('').last)
        m.empty? ? "application/octet-stream" : m.to_s
      end
    end

Can someone help to adjust that at least?
Eefa96eb766ca4dd85141bc738245cda?d=identicon&s=25 salamis salamis (salamis)
on 2011-08-19 15:00
I also found:

http://forrst.com/posts/post_a_photo_to_the_fb_gra...

    class FbPhotoUploader
      require 'net/http/post/multipart' # multipart-post gem
      require 'mime/types' #mime-types gem
      require 'net/https'

      def upload(path, access_token, options={})
        photo = File.open(path)
        params = {:access_token => access_token}
        params.merge!(options)
        url = URI.parse("https://graph.facebook.com/me/photos")

        req = Net::HTTP::Post::Multipart.new
"#{url.path}?#{params.to_query}",
        "file" => UploadIO.new(photo, mime_for_file(photo), photo.path)

        n = Net::HTTP.new(url.host, url.port)
        n.use_ssl = true
        n.verify_mode = OpenSSL::SSL::VERIFY_NONE
        n.start do |http|
          http.request(req)
        end
      end

    private
      def mime_for_file(f)
        m = MIME::Types.type_for(f.path.split('').last)
        m.empty? ? "application/octet-stream" : m.to_s
      end
    end

Can someone help to adjust that at least?
2b94106007474e24fd127f869b01455c?d=identicon&s=25 Adam K. (adam_k)
on 2011-09-14 15:42
Hi,
were you able to come up with a solution?
I have the same problem myself

Thanks
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.