I'm working with my first webhook in my rails application. My HookController is receiving the following response:
Started POST "/hooks/callback" for 127.0.0.1 at 2014-11-18 14:10:13 -0500
Processing by HooksController#callback as HTML
Parameters: {"Status"=>"<?xml version=\"1.0\"?>\n<BackgroundReports userId=\"username\" ...
Completed 401 Unauthorized in 1ms
I'm trying parse the XML that is returned in the parameters and I'm getting stuck.
I have tried starting with the following in my controller raising :
webHook = JSON.parse(params[:status])
raise webHook.inspect
But it appears as if its not capturing the params.
How do I capture the params and parse the XML in my controller?
I was able to troubleshoot the problem myself.
I am using Devise for my User model. That was driving the 401 Unauthorized error. As a result I was not able to to pull and parse the params.
I found the answer in a similar question -> Webhook returning 401 in Rails on Stakoverflow. Adding the following allowed Devise to authenticate the response. I could parse the params from there:
skip_before_filter :verify_authenticity_token, :authenticate_user!
Related
I'm trying to send myself an email with the details of a post request received from a webhook
p = params.permit!
selfMailer.send_myself_an_email(p).deliver_later
However this only send me an email with the content:
{"controller"=>"custom/send", "action"=>"create"}
I tried to call .inspect on the params but not much changed, this is what I got:
"#<ActionController::Parameters {\"controller\"=>\"custom/send\", \"action\"=>\"create\"} permitted: true>"
In the log I can see clearly the parameters passed, there are many, but above is all I get! so how can I access them?
wow! I haven't thought about it, it seems that I made my app to redirect www.example.com to example.com, so basically when the webhook request arrived to the www url it was redirected, that's why I wasn't seeing the post params!
I just adjusted the webhook url by removing the www and now it's working
This question already has answers here:
Rails: Can't verify CSRF token authenticity when making a POST request
(10 answers)
Closed 5 years ago.
I can see the authenticity_token in the form parameters when I post yet I am getting this error message:
Started POST "/helloasdf/destroy_task?id=29" for 127.0.0.1 at 2017-08-19 21:12:14 -0400
Processing by TaskController#destroy_task as JS
Parameters: {"authenticity_token"=>"XXXX", "id"=>"29", "task_url"=>"helloasdf"}
Can't verify CSRF token authenticity.
Completed 422 Unprocessable Entity in 1ms (ActiveRecord: 0.0ms)
(I changed the token to XXXX).
What exactly is the problem or what am I doing wrong?
My ApplicationController has:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
This happened to me a while back, and I just couldn't figure out why it all out of the blue started throwing this error as soon as I tried to write something. After about 5 hours of debugging I realised that I had blocked cookies for localhost:3000, and a wild guess is that you probably have too.
Another guess, assuming you use application/x-www-form-urlencoded content type, is that you are not properly escaping the token - as it is base64 encoded it can contain plus (+) sign which will be interpreted as a space and will fail the authenticity check.
Check also this answer:
Rails 3 Authenticity tokens: How to prevent it from using spaces?
(although despite what the question says, a simple encodeURIComponent() of the token should also suffice)
InvalidURIError (bad URI(is not URI?): https://api.flightstats.com/flex/schedules/rest/v1/json/from/VNS/to/DEL/departing/2017/
8/7?appId=94f56975&appKey=0a0dc2b64f177ab866f0dba59342ffa4)
how can solve this error.useing httparty post method.
flight =HTTParty.post("https://api.flightstats.com/flex/schedules/rest/v1/json/from/#{params[:from]}/to/#{params[:to]}/departing/#{params[:year]}/
#{params[:month]}/#{params[:day]}?appId=94f56975&appKey=0a0dc2b64f177ab866f0dba59342ffa4")
There are two problems. First the URL includes a whitespace between #{params[:year]} and #{params[:month]} which leads to an invalid URL. The code tries a POST request, but according to their documentation that endpoint accepts GET requests.
Change your code to:
HTTParty.get(
"https://api.flightstats.com/flex/schedules/rest/v1/json/from/"{params[:from]}/to/#{params[:to]}/departing/#{params[:year]}/#{params[:month]}/#{params[:day]}?appId=94f56975&appKey=0a0dc2b64f177ab866f0dba59342ffa4"
)
i am using doorkeeper gem
my ApplicationController look like this:
private
def current_resource_owner
Person.find(doorkeeper_token.resource_owner_id) if doorkeeper_token
end
my DemosController look like this:
doorkeeper_for :index
respond_to :json
def index
respond_with current_resource_owner
end
response comes like this:
Started GET "/?code=f88d2e95b1b286645d31772c395e0e36708a5i0p970836af640f631bb4f043b5" for 127.0.0.1 at 2014-01-28 11:10:56 +0530
Processing by DemosController#index as HTML
Parameters: {"code"=>"f88d2e95b1b286645d31135c395e0e36708a5b5b970836af640f631bb4f043b5"}
Filter chain halted as #<Proc:0xb608b90#/home/xyz/.rvm/gems/ruby-1.9.3-p484#verticalserver/gems/doorkeeper-1.0.0/lib/doorkeeper/helpers/filter.rb:8> rendered or redirected
Completed 401 Unauthorized in 1ms (ActiveRecord: 0.0ms)
This is actually an issue on Doorkeeper. To have a custom JSON response for 401 Unauthorized errors instead of a blank page, in ApplicationController I added:
def doorkeeper_unauthorized_render_options
{json: '{"status": "failure", "message":"401 Unauthorized"}'}
end
I'm not 100% sure if I understand your question correctly. Your code looks fine, but the request seems to be wrong.
With Doorkeeper you need an Access Token and not the Code parameter to access the resource (DemosController#index). So first of all you have to get the Access Token from the Authorization Code. Therefore make a request
GET "/oauth/token?code=...&grant_type=authorization_code&redirect_uri=...&client_id=...&client_secret=..."
Make sure that the redirect_uri matches with the one registered with your client application and add the correct client_id and client_secret to the request. Also use always a fresh code parameter. Per default it is only valid for 10 minutes after generation. Notice, in case of customized Doorkeeper routes the url (/oauth/token) might be different.
If you done the request correctly, the response will contain a valid access token.
Then make a GET request to "/index.json?access_token=..." instead of "/?code=...". '.json' tells Rails your client can handle JSON. Otherwise you will get a 406 Response, which means that the requested format (by default HTML) is not supported. Instead of '.json' you can also send Accept="application/json" in the HTTP header.
The 401 Unauthorized response, what you're currently receiving, means that the authentication information (in your case a valid Access Token) is wrong or missing at all.
Hope that helps.
Your respond_to also needs to respond to html because you are requesting "DemosController#index as HTML"
respond_to :html, :json
May not be the answer for you, but it may be the answer for you.
We downgraded our version of doorkeeper and it solved the problem. march2019
https://github.com/doorkeeper-gem/doorkeeper/issues/732
I have a webservice application, which in one of my controllers there's an action that receives a POST from an external application.
Here's the log from my heroku app, which confirms that it's coming a Json data parameter:
2012-01-05T18:26:07+00:00 app[web.1]: Started POST "/let" for 192.168.1.145 at 2012-01-05 10:26:07 -0800
2012-01-05T18:26:07+00:00 app[web.1]: Processing by LetController#create as HTML
2012-01-05T18:26:07+00:00 app[web.1]: Parameters: {"Id"=>"dfe510c1-56e2-48ae-af0d-c4b265798f2e", "FormId"=>"5025", "Answers"=>[{"IdQuestion"=>"cfe0"
, "Value"=>"One", "FilledDate"=>"/Date(1325795160323-0200)/"}], "MSISDN"=>"8984223722"}
So, how can I access this json parameter on my method? I've tried lots of things like params[:formid], params["formid"], ActiveSupport::JSON.decode(request.body) etc etc.. and when pass to view, nothing is rendered, it's like it doesn't get anything.
It looks as though you should be able to access your params as follows:
params[:Id]
params[:FormId]
params[:Answers]
params[:Value]
params[:FilledDate]
params[:MSISDN]