使用curl将JSON发送到RoR时的404 [英] 404 when sending JSON to RoR with curl

查看:122
本文介绍了使用curl将JSON发送到RoR时的404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对RoR还是很陌生,并且正在即时学习.话虽这么说,我试图解析从Formstack发送到我的脚本的POST请求.我可以通过转到URL来查看和运行脚本,它的工作原理很好,当我将Formstack中的Webhook指向URL时,什么也没发生.我从命令行运行了以下curl请求,它返回了404错误.

I'm very new to RoR, and I'm learning on the fly. With that being said, I'm trying to parse a POST request sent from Formstack to my script. I can view and run the script by going to it's URL and it works just fine, put when I point the Webhook in Formstack to the URL, nothing happens. I ran the below curl request from my command line, and it returned a 404 error.

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错误,而在浏览器中查看却能正常运行呢?我的示例代码如下.

I checked the data being sent with requestb.in, and everything is being sent properly. So why would it return a 404 error when JSON data is being sent, but run just fine when I view it in my browser? My sample code is below.

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的内容:

Here is the contents of 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'

推荐答案

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

I found the problem. I had to add the following into config/routes.rb:

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

然后在controllers/application_controller.rb中,您需要替换它:

And then in controllers/application_controller.rb, you need to replace this:

protect_from_forgery with: :exception

与此:

protect_from_forgery with: :null_session

这篇关于使用curl将JSON发送到RoR时的404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆