سؤال

I have a old service and new service with images on it. I want to migrate the images from old to new. for that I need to get image from my old service and then post it to new sinatra service. I was able to post image in the format i want using NET::HTTP

  url = URI.parse('http://0.0.0.0:9292/')
  File.open("./Belgium.png") do |jpg|
   req = Net::HTTP::Post::Multipart.new url.path, {"image" => UploadIO.new(jpg, "image/jpeg", "Belgium.jpg"), id:4234}
    res = Net::HTTP.start(url.host, url.port) do |http|
      http.request(req)
    end
  end

The params that I get with above post method at my sinatra service are

params = {"image"=>
  {:filename=>"Belgium.jpg", :type=>"image/jpeg",:name=>"image",:tempfile=> #<File:/var/folders/h7/f9cvygqs3lg78kwh24v8yzp80000gn/T/RackMultipart20120827-76057-1f3inkn>,
 :head=> "Content-Disposition: form-data; name=\"image\";filename=\"Belgium.jpg\"\r\nContent-Length: 28228\r\nContent-Type: image/jpeg\r\nContent-Transfer-Encoding: binary\r\n"},
  "id"=>"4234"}

But I wasn't able to get the same result with faraday. I tried using something like:

payload = { :profile_pic => Faraday::UploadIO.new('avatar.jpg', 'image/jpeg') }
Faraday.post 'http://0.0.0.0:9292/', payload

At my service I get something like this:
"#<'UploadIO:0x007fb950373de8>" An object. This is not what i expect at my service.

How can I get the same behaviour as I get with Net::HTTP. I think it has go to do something with middleware, I tried tweaking around but I am still a newbie.

هل كانت مفيدة؟

المحلول

What about:

conn = Faraday.new(:url => 'http://0.0.0.0:9292' ) do |faraday|
  faraday.request :multipart
end
payload = { :profile_pic => Faraday::UploadIO.new('avatar.jpg', 'image/jpeg') }
conn.post 'http://0.0.0.0:9292/', payload
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top