gpt4 book ai didi

ruby-on-rails - 在 Rails 4 中接收 POST 数据并读取 request.body

转载 作者:数据小太阳 更新时间:2023-10-29 06:31:34 24 4
gpt4 key购买 nike

我想向 Rails 应用程序发送 POST 请求,并让它在数据库中保存和解析请求主体...

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

post '/request' => 'controller#receives_data'

当我将数据发布到我使用的这个 Controller 时:

def post_it
connection.post(uri.path, "this is data", header_with_authkey)
end

我接收帖子的 Controller 方法设置为:

def receives_data
log(request.body.read)
end

但是我得到了一个 422 错误,无法处理的实体,并且日志文件总是空的...

是否需要包含特定的 header 才能将其发布到 Rails 应用程序?我需要在我的 Controller 或路由中包含特定配置吗?

最佳答案

request.raw_post

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

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

关于ruby-on-rails - 在 Rails 4 中接收 POST 数据并读取 request.body,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19919780/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com