【问题标题】:404 when sending JSON to RoR with curl使用 curl 将 JSON 发送到 RoR 时出现 404
【发布时间】:2015-09-30 23:51:24
【问题描述】:

我对 RoR 很陌生,我正在快速学习。话虽如此,我正在尝试解析从 Formstack 发送到我的脚本的 POST 请求。我可以通过转到它的 URL 查看和运行脚本,它工作得很好,当我将 Formstack 中的 Webhook 指向 URL 时,什么也没有发生。我从命令行运行了以下 curl 请求,它返回了 404 错误。

curl -H "Content-Type: application/json" -d '{"FormID":"1234","UniqueID":"1234","Name":{"first":"John","last":"Smith"},"Email":"test@gmail.com","Phone":"(555) 555-555","Company":"Test Company"}' -X POST "http://scripturl.com"

我检查了使用 requestb.in 发送的数据,并且一切都正确发送。那么为什么在发送 JSON 数据时它会返回 404 错误,但是当我在浏览器中查看它时却运行良好?我的示例代码如下。

firstName = params[:Name]["first"]
lastName = params[:Name]["last"]
email = params["Email"]
company = params["Company"]
phone = params["Phone"]
client.create('contact', firstname: firstName, lastname: lastName, emailaddress1: email, subject: "Test", companyname: company, mobilephone: phone)

这里是log/production.log的内容:

rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
  railties (4.2.3) lib/rails/engine.rb:518:in `call'
  railties (4.2.3) lib/rails/application.rb:165:in `call'
  passenger (5.0.13) lib/phusion_passenger/rack/thread_handler_extension.rb:94:in `process_request'
  passenger (5.0.13) lib/phusion_passenger/request_handler/thread_handler.rb:157:in `accept_and_process_next_request'
  passenger (5.0.13) lib/phusion_passenger/request_handler/thread_handler.rb:110:in `main_loop'
  passenger (5.0.13) lib/phusion_passenger/request_handler.rb:415:in `block (3 levels) in start_threads'
  passenger (5.0.13) lib/phusion_passenger/utils.rb:111:in `block in create_thread_and_abort_on_exception'

【问题讨论】:

  • 您在 rails 日志中遇到什么错误?
  • 使用输出编辑原始帖子

标签: ruby-on-rails json curl formstack


【解决方案1】:

我发现了问题。我必须在 config/routes.rb 中添加以下内容:

post '/add' => 'contacts#add'

然后在controllers/application_controller.rb中,你需要替换这个:

protect_from_forgery with: :exception

有了这个:

protect_from_forgery with: :null_session

【讨论】:

    猜你喜欢
    • 2019-03-04
    • 2022-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多