salamis salamis
2011-Aug-19 13:00 UTC
How to post a file via HTTP as multipart/form-data to Facebook?
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=#{params[: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
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
salamis salamis
2011-Aug-19 13:08 UTC
Re: How to post a file via HTTP as multipart/form-data to Facebook?
I think that this can help :
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
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.