6
votes

I try to work with a webhook to get a JSON, I read that I should install ngrok because webhooks do not work locally, so I installed ngrok, and tried to follow this small tuto : https://medium.com/@derek_dyer/rails-webhooks-local-development-7b7c755d85e3

I created my routes :

get 'invoice/webhooks'
post 'invoice/webhooks' =>'invoice#webhooks'

And my controller :

def webhooks
   render json: response.body, status: 200
end

I also plugged my URL : https://ce0d99f7.ngrok.io/invoice/webhooks in my service to receive the webhook

I run ./ngrok http 3000 in my terminal and I receive a message

POST /invoice/webhooks         403 Forbidden

Is anyone knows how to fix that ?

1
The "message" you mentioned is from the ngrok's output, right? I'd say it works as expected - just forwards the request to the app server. I'd check the app server error log/console output to get more context about the error... - Konstantin Strukov
Thanks for your answer ! Yes the message is from the console where I started ngrok ! - Damien Compère
Is there any kind of authentication on controller on before_action callback? - Shishir
Nope nothing, I did some modification and now I have a 422 Unprocessable Entity for the POST /invoice/webhooks - Damien Compère
Added skip_before_action :verify_authenticity_token in my controller fixed the problem, I now have a 200 OK on my POST /invoice/webhooks - Damien Compère

1 Answers

28
votes

If you using rails 6 and use ngrok in development you must edit the development.rb file in config/environments for add config.hosts << "a0000000.ngrok.io" where a0000000.ngrok.io is the url supplied by ngrok without https:// , if this no work so you must add skip_before_action :verify_authenticity_token in you controller.