Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails API端点接收到请求,但什么也没发生_Ruby On Rails_Ruby_Api_Post_Endpoint - Fatal编程技术网

Ruby on rails API端点接收到请求,但什么也没发生

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

我正在为我的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

以下是发送请求的其他应用程序中的代码:

  @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
失败

尝试/调查的事项:

  • 是否存在阻止请求或限制允许用户使用哪些对象的权限?e、 g.检查CanCan配置,如果您正在使用它或类似的gem

  • 尝试使用
    create而不是
    create
    。这会引起错误吗?可能存在验证或其他必填字段

  • 将生产日志更改为调试级别,或将创建分配给变量
    ,然后注销
    项。错误。完整消息


  • 您确定您的
    path
    方法正在执行吗?您是否检查了
    项上的错误。是否创建
    呼叫?请检查服务器生产日志以了解有关正在进行的操作的更多信息on@muistooshort我的
    path
    方法显然没有被执行,因为在控制台中运行
    Item.create(name:“test”)
    可以正常工作。我不明白为什么它没有被执行,因为我的日志显示收到了post请求。@MartinZinovsky我从Heroku日志中列出的行是日志中给出的唯一反馈。@YuriGert我是说rails应用程序生产日志在
    rails\u root/log/production.log