在Rails 4中接收POST数据并读取request.body

Dav*_*vey 19 ruby post routes ruby-on-rails http

我想向rails应用程序发送POST请求,并将其保存并解析数据库中的请求主体...

我在接收端的路由目前设置为:

post '/request' => 'controller#receives_data'
Run Code Online (Sandbox Code Playgroud)

当我将数据发布到此控制器时,我使用:

def post_it
  connection.post(uri.path, "this is data", header_with_authkey)
end
Run Code Online (Sandbox Code Playgroud)

接收帖子的我的控制器方法设置为:

def receives_data
   log(request.body.read)
end
Run Code Online (Sandbox Code Playgroud)

但是我得到了422 error, unprocessable entity,日志文件总是空的...

是否需要包含特定标头才能将其发布到rails应用程序?我需要在控制器或路由中包含哪些特定配置?

fei*_*ang 24

request.raw_post
Run Code Online (Sandbox Code Playgroud)

阅读请求正文.这对于需要直接处理原始请求的Web服务非常有用.

http://api.rubyonrails.org/classes/ActionDispatch/Request.html#method-i-raw_post


小智 19

您需要在帖子中设置以下标题.

Content-Type: application/json
Accept: application/json
Run Code Online (Sandbox Code Playgroud)