访问控制器中的创建方法时出现 WEBrick::HTTPStatus::LengthRequired 错误

发布于 2024-08-28 23:12:50 字数 2209 浏览 4 评论 0原文

我设置了一个非常简单的控制器:

class LibrariesController < ApplicationController

...

  def create
    @user.libraries << Library.new(params)
    @user.save
    render :json => "success!"
  end

...

end

基本上,每当我尝试使用 HTTParty.post 访问 LibrariesController 的创建方法时,我都会在服务器上收到 WEBrick::HTTPStatus::LengthRequired 错误。该方法甚至没有被访问!这是堆栈跟踪(这是完整的输出服务器端 - 请注意,控制器甚至没有被访问):

[2010-04-16 00:35:39] ERROR WEBrick::HTTPStatus::LengthRequired
[2010-04-16 00:35:39] ERROR HTTPRequest#fixup: WEBrick::HTTPStatus::LengthRequired occured.
[2010-04-16 00:35:39] ERROR NoMethodError: private method `gsub!' called for #<Class:0x2362160>
    /usr/local/Cellar/ruby_187/1.8.7-p249/lib/ruby/1.8/webrick/htmlutils.rb:17:in `escape'
    /usr/local/Cellar/ruby_187/1.8.7-p249/lib/ruby/1.8/webrick/httpresponse.rb:232:in `set_error'
    /usr/local/Cellar/ruby_187/1.8.7-p249/lib/ruby/1.8/webrick/httpserver.rb:70:in `run'
    /usr/local/Cellar/ruby_187/1.8.7-p249/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
    /usr/local/Cellar/ruby_187/1.8.7-p249/lib/ruby/1.8/webrick/server.rb:162:in `start'
    /usr/local/Cellar/ruby_187/1.8.7-p249/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
    /usr/local/Cellar/ruby_187/1.8.7-p249/lib/ruby/1.8/webrick/server.rb:95:in `start'
    /usr/local/Cellar/ruby_187/1.8.7-p249/lib/ruby/1.8/webrick/server.rb:92:in `each'
    /usr/local/Cellar/ruby_187/1.8.7-p249/lib/ruby/1.8/webrick/server.rb:92:in `start'
    /usr/local/Cellar/ruby_187/1.8.7-p249/lib/ruby/1.8/webrick/server.rb:23:in `start'
    /usr/local/Cellar/ruby_187/1.8.7-p249/lib/ruby/1.8/webrick/server.rb:82:in `start'
    /usr/local/Cellar/ruby_187/1.8.7-p249/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/handler/webrick.rb:14:in `run'
    /usr/local/Cellar/ruby_187/1.8.7-p249/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/commands/server.rb:111
    /usr/local/Cellar/ruby_187/1.8.7-p249/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    /usr/local/Cellar/ruby_187/1.8.7-p249/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
    script/server:3

我正在运行 Rails 2.3.5 和 ruby​​ 1.8.7。任何帮助将不胜感激。如果您需要更多详细信息,请告诉我。

I have a very simple controller set up:

class LibrariesController < ApplicationController

...

  def create
    @user.libraries << Library.new(params)
    @user.save
    render :json => "success!"
  end

...

end

Basically, whenever I try to access the create method of LibrariesController using HTTParty.post I get a WEBrick::HTTPStatus::LengthRequired error on the server. The method is not even being accessed! Here is the stack trace (this is the full output server side - notice that the controller isn't even being accessed):

[2010-04-16 00:35:39] ERROR WEBrick::HTTPStatus::LengthRequired
[2010-04-16 00:35:39] ERROR HTTPRequest#fixup: WEBrick::HTTPStatus::LengthRequired occured.
[2010-04-16 00:35:39] ERROR NoMethodError: private method `gsub!' called for #<Class:0x2362160>
    /usr/local/Cellar/ruby_187/1.8.7-p249/lib/ruby/1.8/webrick/htmlutils.rb:17:in `escape'
    /usr/local/Cellar/ruby_187/1.8.7-p249/lib/ruby/1.8/webrick/httpresponse.rb:232:in `set_error'
    /usr/local/Cellar/ruby_187/1.8.7-p249/lib/ruby/1.8/webrick/httpserver.rb:70:in `run'
    /usr/local/Cellar/ruby_187/1.8.7-p249/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
    /usr/local/Cellar/ruby_187/1.8.7-p249/lib/ruby/1.8/webrick/server.rb:162:in `start'
    /usr/local/Cellar/ruby_187/1.8.7-p249/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
    /usr/local/Cellar/ruby_187/1.8.7-p249/lib/ruby/1.8/webrick/server.rb:95:in `start'
    /usr/local/Cellar/ruby_187/1.8.7-p249/lib/ruby/1.8/webrick/server.rb:92:in `each'
    /usr/local/Cellar/ruby_187/1.8.7-p249/lib/ruby/1.8/webrick/server.rb:92:in `start'
    /usr/local/Cellar/ruby_187/1.8.7-p249/lib/ruby/1.8/webrick/server.rb:23:in `start'
    /usr/local/Cellar/ruby_187/1.8.7-p249/lib/ruby/1.8/webrick/server.rb:82:in `start'
    /usr/local/Cellar/ruby_187/1.8.7-p249/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/handler/webrick.rb:14:in `run'
    /usr/local/Cellar/ruby_187/1.8.7-p249/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/commands/server.rb:111
    /usr/local/Cellar/ruby_187/1.8.7-p249/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    /usr/local/Cellar/ruby_187/1.8.7-p249/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
    script/server:3

I'm running rails 2.3.5 and ruby 1.8.7. Any help would be greatly appreciated. Let me know if you need more details.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

鹤舞 2024-09-04 23:12:50

当我用空的主体进行 POST 时,我得到了这个。我正在使用卷曲。

比如:

curl -X POST http://url/

我添加了 -d '' ,它解决了这个问题。

curl -X POST http://url/ -d ''

在您的情况下,您可能需要向 :body => 添加一些内容HTTParty.post 中的 '' 属性

I got this when I did a POST with an empty body. I was using curl.

Something like:

curl -X POST http://url/

I added -d '' and it cleared up the issue.

curl -X POST http://url/ -d ''

In your case, you probably need to add some content to a :body => '' attribute in HTTParty.post

烟雨扶苏 2024-09-04 23:12:50

在 post 和 put 请求中使用 :body 而不是 :query

use :body instead of :query on post and put requests.

歌入人心 2024-09-04 23:12:50

我不知道这是否仍然与 Webrick 相关,或者是否已修复,但您收到的错误有一个已知问题 阅读 RubyForge 上此 TODO 页面上的第 2 行

您是否尝试过 gem install mongrel 并使用 mongrel 或其他 ruby​​ 服务器运行它?

I don't know if this is still relevant for Webrick or if that has been fixed but there is a know issue for the error you are getting Read line 2 on this TODO page on RubyForge

Have you tried gem install mongrel and running it with mongrel or other ruby server?

掀纱窥君容 2024-09-04 23:12:50

我也遇到同样的问题,现在已经解决了。对我来说,这是请求标头值中换行符的问题。一旦 WEBrick 服务器收到浏览器或其他客户端的请求,它就会将该请求发送到您的应用程序,并立即返回代码 411 和ERROR WEBrick::HTTPStatus::LengthRequired

因此,在放置标头值之前,您可以删除换行符,仅此而已。

I was too having the same problem, just now I have solved it. With me it was the problem of newline char in the request header value. As soon as the WEBrick server gets the request form browser or and other client it will sends the request to your application and immediately returns code 411 withERROR WEBrick::HTTPStatus::LengthRequired.

So Before Putting the header value you can remove newline char that's all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文