1

我对 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'
4

1 回答 1

1

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

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

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

protect_from_forgery with: :exception

有了这个:

protect_from_forgery with: :null_session
于 2015-07-12T19:21:55.680 回答