rubymultipart

ruby multipart post image with digest auth


Given I have this, using Ruby 1.9.3p194

Authentication is digestauth

require 'json'
require 'httpclient'

API_URL= "https://api.somewhere.com/upload"
API_KEY='blahblah'
API_SECRET ='blahlbah'
IMAGE ='someimage.png'

h=HTTPClient.new
h.set_auth(API_URL, API_KEY, API_SECRET)

File.open(IMAGE) do |file|
  body = { 'image' => file}
  res = h.post(API_URL, body)
  p res.inspect
end

I get errors

I've tried Typheous, Patron, Mechanize, Curl but want to find a way that is simple and works e.g.

curl --digest -u myusrname:password  -F "image=@image.png" "https://api.somewhere.com/upload"

Curl posts nothing and doesn't work as expected. I've been assured that the API accepts posts, I have a simple web page that does what I need to do via a simple form and it works fine

Any one know what the easiest way ahead is?


Solution

  • Solved it, went back to Curb. It is a RESTful API, RestClient was doing something funky with the digest. HttpClient too was posting blank files. Curb did it.