JSON parsing from webhook request

Using https://requestb.in , I see that the webcam is sending the + json header data correctly. But when I send a json request to my server, I get a json error message.

My controller (cannot receive body data):

 class ReceiverController < ApplicationController skip_before_filter :verify_authenticity_token def handle_post puts request.headers['Content-Type'] puts "request:" puts JSON.parse(request.raw_post) puts "request2:" puts JSON.parse(request.body.read) end end 

Error output:

 application/json; charset=utf-8 request: JSON::ParserError (A JSON text must at least contain two octets!): app/controllers/receiver_controller.rb:69:in `handle_post' request2: Completed 500 Internal Server Error in 7ms (ActiveRecord: 0.0ms) 

routes.rb

  post "/receive" => 'receiver#handle_post' 
+7
ruby ruby-on-rails webhooks
source share
1 answer

I think the rails are blocking the request because of the CSRF protection that the rails provide, I'm new to webhooks bands , but their webhooks documentation advised me to do the following ( https://stripe.com/docs/webhooks ):

If you use Rails, Django, or another web infrastructure, your site can automatically verify that every POST request contains a CSRF token. This is an important security feature that helps protect you and your users from trying to fake cross-site request requests. However, this security measure may also prevent your site from processing legitimate webcams. If so, you may need to free the webhooks route from CSRF protection.

 class ReceiverController < ApplicationController # If your controller accepts requests other than webhooks, # you'll probably want to use `protect_from_forgery` to add CSRF # protection for your application. But don't forget to exempt # your webhook route! protect_from_forgery :except => :handle_post def handle_post # Process webhook data in `params` end end 
0
source share

All Articles