With curl
As is
The obvious answer would be to just call the given command from Ruby :
curl_command = %q(curl -F metadata='{"userID":"12345","mimeType":"image/jpeg","typeHint": "uploadedImage" }' -F contents=@$HOME/Downloads/photo.jpg http://localhost:8083/myservice/upload)
system(curl_command)
%q()
is used to define a string since you have many characters that would need to be unescaped otherwise (e.g. "
or '
)
with parameters
If you want dynamic parameters, you could use :
def curl_command(user_id, file, url)
%Q(curl -F metadata='{"userID":"#{user_id}","mimeType":"image/jpeg","typeHint": "uploadedImage" }' -F contents=#{file} #{url})
end
picture = File.join(Dir.home, 'Downloads', 'photo.jpg')
puts curl_command(12345, picture, "http://localhost:8083/myservice/upload")
#=> curl -F metadata='{"userID":"12345","mimeType":"image/jpeg","typeHint": "uploadedImage" }' -F contents=/home/eric/Downloads/photo.jpg http://localhost:8083/myservice/upload
system(curl_command(12345, picture, "http://localhost:8083/myservice/upload"))
With curb
It looks like the curb
gem can do what you want :
HTTP POST file upload:
c = Curl::Easy.new("http://my.rails.box/files/upload")
c.multipart_form_post = true
c.http_post(Curl::PostField.file('thing[file]', 'myfile.rb'))