ruby - Redmine/Rails - Undefined method while sending POST data -
redmine allows user add files document manually, goal create method automatically.
i've added file manually while sniffing wireshark post requests want recreate in method me little bit (i can't post screenshots (if needed), reputation low).
redmine official website offers informations how attach files here: http://www.redmine.org/projects/redmine/wiki/rest_api#attaching-files
so after hours browsing web , particulary stackoverflow , here, wrote 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 redmine website specified content-type in header , added file in body request.
now following error:
nomethoderror (undefined method `+' 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 uri absolute url this:
uri = uri.parse("http://<server_ip_address>:3000/uploads.js")
and i'm getting following error:
nomethoderror (undefined method `bytesize' #<file:/home/testfile.txt>):
do see error in method or have idea error come from?
note: i'm using rails 3.2.16 , ruby 1.9.3-p392
as far know, cannot use relative uri line
uri = uri.parse('/uploads.js')
cannot correctly processed. try using absolute url ! , work.
i believe instead of
request.body = file.new(...)
you should use
request.body = file.read(...)
Comments
Post a Comment