【问题标题】:Can't get 200 status code from post request to Rails route无法从发布请求到 Rails 路由中获取 200 状态代码
【发布时间】:2015-01-07 15:55:39
【问题描述】:

我的 Pizzas 控制器中有这个:

def post
  render :nothing => true, status: 200 
end

def get
  render :nothing => true, status: 200 
end

我的路线:

 get '/' => 'pizzas#get'
 post '/' => 'pizzas#post'

这就是我发布到该路线的方式:

require 'httparty'
response = HTTParty.post('https://example.com', {})
p response.code #returns 422 (Unprocessable Entity)

这是什么原因?通过 GET 对该路线的请求,我可靠地得到了200。如何获取代码200

【问题讨论】:

  • 您要发布到应用程序所在的位置,而不是控制器中的发布操作。你的路线有什么?
  • @japed,我做到了,谢谢提醒,我现在将我的路线添加到问题中。
  • 你在哪里发帖?它需要你发送 JSON 吗?
  • @neo,不,它不需要我发送 JSON。顺便说一句,如果有帮助的话 - Pizzas 控制器中有一个before_filter,它调用一个与 websockets 对话并在数据库中创建记录的方法。
  • 你能显示你在 before_filter 上调用的函数吗?

标签: ruby-on-rails ruby http httparty


【解决方案1】:

这表明我必须删除 application_controller.rb 中的 protect_from_forgery 行。 GET 请求正在通过,因为它不需要真实性令牌(您在 rails 助手创建的表单中看到的那个)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-18
    相关资源
    最近更新 更多