3

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

3
  • 1
    Your URI is just a filename. When creating the Net::HTTP object, you are calling its port. This might be nil, so the HTTP class clould be unable to build the request properly when the supplied port is nil. Commented Mar 24, 2014 at 14:15
  • Please print uri.host and uri.port before assigning http and post their content if those are not nil. Commented Mar 24, 2014 at 14:20
  • Both are nil. Should I defined these variables by myself so ? I assume the port is 80 and the host would be redmine.org? Commented Mar 24, 2014 at 14:34

1 Answer 1

1

As far as I know, you cannot use a relative URI so your line

uri = URI.parse('/uploads.js')

cannot be correctly processed. Try using the absolute URL ! And it will probably work.

I believe that instead of

request.body = File.new(...)

You should use

request.body = File.read(...)
Sign up to request clarification or add additional context in comments.

6 Comments

I updated my uri like this: uri = URI.parse("http://<server_IP_address>:3000/uploads.js") because I'm working on a distant server where I've installed Redmine and I'm now getting another error:(undefined method bytesize' for #<File:/home/testFile.txt>`. Another problem is if someone install my plugin on his Redmine platform the code must be modified that's why I put a relative address.
You can also find the current url in another way I guess. If you are in a controller, you can for instance use request.original_url And I updated my answer
Ok I didn't know that, thanks. I'll update my post with the new informations and with the new error message.
Are you sure there is a file at /home/testFile.txt? This error message looks like it would not be there.
Finally I solved the problem, thanks to you, that gave me hints to continue. The request doesn't work tough, I face another problem (CSRF Token stuff) but I'm gonna do a new post for that. Thanks alot.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.