【发布时间】:2018-05-13 04:15:55
【问题描述】:
我正在为我的 Rails 应用程序设置 API,并尝试接收来自其他应用程序的 POST 请求。我已经设法从另一个应用程序远程发送一个 Post 请求,但接收请求的方法没有任何反应。
这是我的 Heroku 日志,显示请求已收到:
2018-05-13T04:55:54.456743+00:00 heroku[router]: at=info method=POST path="/path" host=www.example.com request_id=e0adgss69-0fbd-4597-8fse- f40d344c55f fwd="xx.xx.xxx.xx" dyno=web.1 connect=0ms service=367ms status=422 bytes=93468 protocol=http
这是发送请求的另一个应用程序中的代码:
@items = []
post_url = "http://www.example.com/path"
post_uri = URI(post_url)
http = Net::HTTP.new(post_uri.host, post_uri.port)
req = Net::HTTP::Post.new(post_uri, 'Content-Type' => 'application/json')
req.body = {items: @items}.to_json
res = http.request(req)
puts "response #{res.body}"
这是我接收请求的方法:
def path
Item.create(name: "test")
end
但是发送请求后,我的第一个应用程序没有名为“test”的项目。但我知道请求已收到。当我在控制台中运行 Item.create(name: "test") 时,它成功了。怎么回事?
【问题讨论】:
-
您确定您的
path方法正在执行吗?您检查了Item.create调用中的错误吗? -
检查服务器生产日志以了解更多信息
-
@muistooshort 我的
path方法显然没有被执行,因为在控制台中运行Item.create(name: "test")工作正常。我不明白为什么它没有被执行,因为我的日志显示已收到发布请求。 -
@MartinZinovsky 我从 Heroku 日志中列出的行是日志中给出的唯一反馈。
-
@YuriGert 我的意思是
rails_root/log/production.log中的rails应用生产日志
标签: ruby-on-rails ruby api post endpoint