问题
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"
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)
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'
回答1:
I found the problem. I had to add the following into config/routes.rb:
post '/add' => 'contacts#add'
And then in controllers/application_controller.rb, you need to replace this:
protect_from_forgery with: :exception
With this:
protect_from_forgery with: :null_session
来源:https://stackoverflow.com/questions/31371113/404-when-sending-json-to-ror-with-curl