我对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'
答案 0 :(得分:1)
我发现了问题。我不得不在config / routes.rb中添加以下内容:
post '/add' => 'contacts#add'
然后在controllers / application_controller.rb中,你需要替换它:
protect_from_forgery with: :exception
有了这个:
protect_from_forgery with: :null_session