Ruby on rails API端点接收到请求,但什么也没发生
我正在为我的Rails应用程序设置API,并尝试从其他应用程序接收POST请求。我已经设法从另一个应用程序远程发送Post请求,但是接收请求的方法没有发生任何事情 这是我的Heroku日志,显示已收到请求: 2018-05-13T04:55:54.456743+00:00 heroku[路由器]: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 以下是发送请求的其他应用程序中的代码:Ruby on rails API端点接收到请求,但什么也没发生,ruby-on-rails,ruby,api,post,endpoint,Ruby On Rails,Ruby,Api,Post,Endpoint,我正在为我的Rails应用程序设置API,并尝试从其他应用程序接收POST请求。我已经设法从另一个应用程序远程发送Post请求,但是接收请求的方法没有发生任何事情 这是我的Heroku日志,显示已收到请求: 2018-05-13T04:55:54.456743+00:00 heroku[路由器]:at=info method=POST path=“/path”host=www.example.com request_id=e0adgss69-0fbd-4597-8fse-f40d344c55f
@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
下面是我接收请求的方法:
@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”)
时,它成功了。发生了什么事?注意日志中返回的422
状态代码。这意味着Item.create
失败
尝试/调查的事项:
create控制台中的代码>而不是create
。这会引起错误吗?可能存在验证或其他必填字段
项
,然后注销项。错误。完整消息
您确定您的
path
方法正在执行吗?您是否检查了项上的错误。是否创建呼叫?请检查服务器生产日志以了解有关正在进行的操作的更多信息on@muistooshort我的path
方法显然没有被执行,因为在控制台中运行Item.create(name:“test”)
可以正常工作。我不明白为什么它没有被执行,因为我的日志显示收到了post请求。@MartinZinovsky我从Heroku日志中列出的行是日志中给出的唯一反馈。@YuriGert我是说rails应用程序生产日志在rails\u root/log/production.log