Webrick / Rails - 发送POST请求后出现超时错误

时间:2014-04-02 07:32:13

标签: ruby-on-rails ruby http webrick redmine-plugins

我正在研究一个Redmine插件,我创建了一个方法,我发送2个HTTP POST请求将文件附加到文档。

执行第一个请求后,虽然请求已成功完成,但我得到Timeout::Error (Timeout::Error),这里是Webrick的错误代码

Timeout::Error (Timeout::Error): 
/usr/local/lib/ruby/1.9.1/net/protocol.rb:146:in `rescue in rbuf_fill' 
/usr/local/lib/ruby/1.9.1/net/protocol.rb:140:in `rbuf_fill' 
/usr/local/lib/ruby/1.9.1/net/protocol.rb:122:in `readuntil' 
/usr/local/lib/ruby/1.9.1/net/protocol.rb:132:in `readline' 
/usr/local/lib/ruby/1.9.1/net/http.rb:2562:in `read_status_line' 
/usr/local/lib/ruby/1.9.1/net/http.rb:2551:in `read_new' 
/usr/local/lib/ruby/1.9.1/net/http.rb:1319:in `block in transport_request'  
/usr/local/lib/ruby/1.9.1/net/http.rb:1316:in `catch' 
/usr/local/lib/ruby/1.9.1/net/http.rb:1316: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'
...
...
Started POST "/uploads.json?attachment_id=1&filename=testFile.txt" for <server_IP_address> at 2014-04-02 09:15:28 +0200 
Processing by AttachmentsController#upload as JSON 
Parameters: {"attachment_id"=>"1", "filename"=>"testFile.txt"} 
Current user: <user> (id=3) 
Saving attachment '/home/user/Redmine/redmine-2.4.2/files/2014/04/140402091528_testFile.txt' (72 bytes) 
Rendered attachments/upload.api.rsb (1.7ms) 
Completed 201 Created in 87.9ms (Views: 7.9ms | ActiveRecord: 11.4ms)

你可以看到我在响应中获得了201状态,因此它有效但我有超时错误并且执行停止。

这是我的代码:

uri = URI.parse("http://<server_IP_address>:3000/uploads.json")

http = Net::HTTP.new(uri.host, uri.port)
@file = File.new("/home/testFile.txt")
@csrfToken = session[:_csrf_token]

request = Net::HTTP::Post.new(@uri.path+"?attachment_id=1&filename=testFile.txt", initheader = {'Content-Type' => "application/octet-stream", 'X-CSRF-Token' => @csrfToken, 'X-Redmine-API-Key' => "my_key"})
request.body = @file.read
@response = http.request(request)

@upToken = @response.body

@secondResponse = RestClient.post 'http://<server_IP_address>:3000/documents/150/add_attachment', {:multipart => true, :utf8 => "\u2713", :authenticity_token => @csrfToken, :attachment => { "1" => {:filename => "testFile.txt", :description => "Dropbox blabla", :token => @upToken}}, :commit => "Add"}, :'X-Redmine-API-Key' => "mykey"

我也尝试过使用Apache Web服务器,第一个请求工作正常,我只能在Webrick服务器上运行Redmine时出现这个问题。

你知道这个错误来自何处以及如何摆脱它?

1 个答案:

答案 0 :(得分:0)

您需要做的是您需要更改webrick服务器中的请求超时值,以便检查此问题的答案: how to set request timeout in rails ( thin or webrick server)

相关问题