发送空POST请求时,为什么会出现内部错误500

时间:2015-05-22 13:51:09

标签: ruby-on-rails http post

我在Rails 4.0中开发API并尝试使用Postman进行测试。我不断获得500状态代码作为POST请求的答案与空体。

如果我使用正确的URL,则无关紧要。在rails中处理空的POST请求似乎是不可能的。当我用PUT,DELETE等尝试它时没有问题。

请求:

POST /whatever HTTP/1.1
Host: localhost:3000
Cache-Control: no-cache
Postman-Token: c479c760-214d-93f0-3a49-2a11c95758f4
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

回复标题:

Connection
Options that are desired for the connection
close
Content-Length
The length of the response body in octets (8-bit bytes)
296
Content-Type
The mime type of this content
text/html; charset=ISO-8859-1
Date → Fri, 22 May 2015 13:57:57 GMT
Server → WEBrick/1.3.1 (Ruby/2.2.1/2015-02-26)

回应机构

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<HTML>
  <HEAD><TITLE>Internal Server Error</TITLE></HEAD>
  <BODY>
    <H1>Internal Server Error</H1>
    bad content body
    <HR>
    <ADDRESS>
     WEBrick/1.3.1 (Ruby/2.2.1/2015-02-26) at
     localhost:3000
    </ADDRESS>
  </BODY>
</HTML>

服务器日志:

[2015-05-22 16:12:57] ERROR EOFError: bad content body
    /Users/fufu/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/multipart/parser.rb:96:in `block in fast_forward_to_first_boundary'
    /Users/fufu/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/multipart/parser.rb:94:in `loop'
    /Users/fufu/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/multipart/parser.rb:94:in `fast_forward_to_first_boundary'
    /Users/fufu/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/multipart/parser.rb:53:in `parse'
    /Users/fufu/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/multipart.rb:25:in `parse_multipart'
    /Users/fufu/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/request.rb:373:in `parse_multipart'
    /Users/fufu/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/request.rb:207:in `POST'
    /Users/fufu/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/methodoverride.rb:39:in `method_override_param'
    /Users/fufu/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/methodoverride.rb:27:in `method_override'
    /Users/fufu/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/methodoverride.rb:15:in `call'
    /Users/fufu/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/runtime.rb:18:in `call'
    /Users/fufu/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
    /Users/fufu/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/lock.rb:17:in `call'
    /Users/fufu/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/static.rb:113:in `call'
    /Users/fufu/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/sendfile.rb:113:in `call'
    /Users/fufu/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/engine.rb:518:in `call'
    /Users/fufu/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/application.rb:164:in `call'
    /Users/fufu/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/lock.rb:17:in `call'
    /Users/fufu/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/content_length.rb:15:in `call'
    /Users/fufu/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/rack-1.6.1/lib/rack/handler/webrick.rb:89:in `service'
    /Users/fufu/.rbenv/versions/2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
    /Users/fufu/.rbenv/versions/2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
    /Users/fufu/.rbenv/versions/2.2.1/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'

1 个答案:

答案 0 :(得分:2)

我也遇到了这个问题,当没有定义POST参数时,似乎Postman正在发送empty body。这会使rails多部分解析器运行,因为没有----WebKitFormBoundary7MA4YWxkTrZu0gW boundary delimiter应该包围请求的主体,因此意外的EOF。解决方案是转而使用x-www-form-urlencoded(邮递员身上的4个选项之一)。