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 ?
The 403 Forbidden Error happens when the web page (or another resource) that you're trying to open in your web browser is a resource that you're not allowed to access. It's called a 403 error because that's the HTTP status code that the webserver uses to describe that kind of error.
When an Ngrok connection reaches this length it stops working, and the only way to restore it is by stopping and restarting the ngrok command, which causes a new randomly generated URL to be used. You can always check the Ngrok output to see when a connection is due to expire.
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.
@daniel0318's answer is exactly right. I just wanted to mention you can use this regular expression to allow all traffic through ngrok (add to development.rb
):
# /config/environments/development.rb
config.hosts << /[a-z0-9-]+\.ngrok\.io/
Restart your server, and it will work.
One last note: if you think you already have this regex in place, please look carefully, since ngrok URL formats changed some time in the past few months, and now contain -
characters. Tl;dr, config.hosts << /[a-z0-9-]+\.ngrok\.io/
works, but the old regex: config.hosts << /[a-z0-9]+\.ngrok\.io/
(same but not allowing dashes) will not work!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With