Redmine allows the user to add files to a document manually, my goal is to create a method that does that automatically.
I've added a file manually while sniffing with Wireshark to get the POST requests that I want to recreate in my method to help me a little bit (I can't post some screenshots (if needed), my reputation is too low).
Redmine official website offers informations to how attach files here: http://www.redmine.org/projects/redmine/wiki/Rest_api#Attaching-files
So after hours browsing the Web and particulary StackOverflow and here, I finally wrote a method:
require 'net/http'
require 'uri'
uri = URI.parse("/uploads.js")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.path, initheader = {'Content-Type' => 'application/octet-stream'})
request.body = File.new("/home/testFile.txt", 'rb')
@response = http.request(request)
As explained on the Redmine website I specified the content-type in the header and I added the file in the body request.
Now I get the following error:
NoMethodError (undefined method `+' for nil:NilClass):
/usr/local/lib/ruby/1.9.1/net/http.rb:1404:in `addr_port'
/usr/local/lib/ruby/1.9.1/net/http.rb:1339:in `begin_transport'
/usr/local/lib/ruby/1.9.1/net/http.rb:1315:in `transport_request'
/usr/local/lib/ruby/1.9.1/net/http.rb:1293:in `request'
rest-client (1.6.7) lib/restclient/net_http_ext.rb:51:in `request'
/usr/local/lib/ruby/1.9.1/net/http.rb:1286:in `block in request'
/usr/local/lib/ruby/1.9.1/net/http.rb:745:in `start'
/usr/local/lib/ruby/1.9.1/net/http.rb:1284:in `request'
rest-client (1.6.7) lib/restclient/net_http_ext.rb:51:in `request'
plugins/redmine_reddrop/app/controllers/projectusers_controller.rb:360:in `addFile'
EDIT:
I updated my uri with an absolute URL like this:
uri = URI.parse("http://<server_IP_address>:3000/uploads.js")
And now I'm getting the following error:
NoMethodError (undefined method `bytesize' for #<File:/home/testFile.txt>):
Do you see any error in my method or do you have any idea where does this error come from?
Note: I'm using rails 3.2.16 and Ruby 1.9.3-p392