Project

General

Profile

Undefined method while sending POST data

Added by Kevin Tomba over 10 years ago

Version used:

Redmine 2.4.2
Ruby 1.9.3-p392
Rails 3.2.16

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 followed the informations to how attach files here: http://www.redmine.org/projects/redmine/wiki/Rest_api#Attaching-files

So after hours browsing the Web, 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 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'

Do you see any error in my method or do you have any idea where does this error come from?