glarkou
glarkou

Reputation: 7101

Rails: 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

Upvotes: 4

Views: 6168

Answers (2)

Feuda
Feuda

Reputation: 2365

How to send the params[:message] with the file in order to have a caption for the image?

In Rails 4, post files with other normal params such as input values should perform as belows

def model_params
  require_params = params.require(:model).permit(:param_one, :param_two, :param_three, :avatar)
  require_params[:avatar] = model_params[:avatar].present? ? UploadIO.new(model_params[:avatar].tempfile,  model_params[:avatar].content_type, model_params[:avatar].original_filename) : nil
  require_params
end

require 'net/http/post/multipart'

url = URI.parse('http://www.example.com/upload')
Net::HTTP.start(url.host, url.port) do |http|
  req = Net::HTTP::Post::Multipart.new(url, model_params)
  key = "authorization_key"
  req.add_field("Authorization", key)
  http.use_ssl = (url.scheme == "https")
  http.request(req)
end

In your case, just

req = Net::HTTP::Post::Multipart.new(url.path, 
      { 
        "file" => UploadIO.new(params[:image].tempfile,  
                               params[:image].content_type,
                               params[:image].original_filename),
        "message" => params[:message]
      })

as simpler

params[:image] = UploadIO.new(params[:image].tempfile,  
                              params[:image].content_type,
                              params[:image].original_filename)
req = Net::HTTP::Post::Multipart.new(url.path, params)

https://github.com/Feuda/multipart-post/tree/patch-1

Upvotes: 0

Andy Lindeman
Andy Lindeman

Reputation: 12165

You already have access to a File instance (well, a TempFile instance) with params[:image].tempfile

Trash all the File.open stuff and simply construct an UploadIO with that:

UploadIO.new(params[:image].tempfile, ...)

Upvotes: 1

Related Questions