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:
- It is not sending the data.
- 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
You already have access to a
Fileinstance (well, aTempFileinstance) withparams[:image].tempfileTrash all the
File.openstuff and simply construct anUploadIOwith that: