Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何确保Rails控制器将发布到它的数据作为JSON进行处理?_Ruby On Rails_Ruby_Json_Controller_Webhooks - Fatal编程技术网

Ruby on rails 如何确保Rails控制器将发布到它的数据作为JSON进行处理?

Ruby on rails 如何确保Rails控制器将发布到它的数据作为JSON进行处理?,ruby-on-rails,ruby,json,controller,webhooks,Ruby On Rails,Ruby,Json,Controller,Webhooks,我正在设置一个url来接收来自第三方API的Webhook,但是我正在努力按照API的要求验证我的url。API会将带有验证令牌的JSON数据发布到我的url,但当我看到帖子出现时,Rails表示控制器正在将其作为文本而不是JSON进行处理,并且Rails似乎试图在我的控制器有机会与之交互之前将JSON转换为哈希 以下是我在发出post请求时看到的内容: Processing by WebhooksController#receive as TEXT Parameters: {"

我正在设置一个url来接收来自第三方API的Webhook,但是我正在努力按照API的要求验证我的url。API会将带有验证令牌的JSON数据发布到我的url,但当我看到帖子出现时,Rails表示控制器正在将其作为文本而不是JSON进行处理,并且Rails似乎试图在我的控制器有机会与之交互之前将JSON转换为哈希

以下是我在发出post请求时看到的内容:

    Processing by WebhooksController#receive as TEXT
    Parameters: {"id"=>"5bb2181f-8b7d-4ba2-80a3-072818bb5310", "type"=>"webhook.verify", "created"=>"2014-09-10T22:36:59Z", "data"=>{"token"=>"CWkrAwoPITotLOPQtaiRosOVHPKiOEyh"}, "webhook"=>{"id"=>"5bb2181f-8b7d-4ba2-80a3-072818bb5310", "type"=>"webhook.verify", "created"=>"2014-09-10T22:36:59Z", "data"=>{"token"=>"CWkrAwoPITotLOPQtaiRosOVHPKiOEyh"}}}
由于上述内容无效,导致:

    JSON::ParserError (757: unexpected token at ''):
    app/controllers/webhooks_controller.rb:24:in `receive'
我已经用Postman Chrome扩展测试了下面的控制器,当发布类似于我将收到的JSON时,它会成功地在响应头中返回令牌

我知道第三方API可能没有将内容类型设置为“application/json”,这可能会导致这种情况,但我如何确保Rails将数据处理为json而不是文本

控制器:

    class WebhooksController < ApplicationController

      def receive
        raw_body = request.body.read
        json = JSON.parse raw_body
        # Return verification token only once for intitial webhook setup.
        token = json['data']['token']

        response.headers["token"]= token
        render :nothing => true
      end

    end
class WebhooksControllertrue
结束
结束

谢谢大家!

尽管我仍然不能完全确定rails在这种情况下是如何工作的,但我已经找到了解决问题的方法。尽管我在问题中发布的控制器代码返回了我在Chrome Postman扩展中寻找的结果,但它给了我一个JSON解析错误,我正试图为其集成webhooks。我改为使用curl进行本地测试,并意识到我得到了相同的错误

以下是要在本地测试的旋度:

    curl -X POST -H "Content-type: application/json" -H "Accept: application/json" -d '{"id":"evt_REPLACEME"}' localhost:3000/your-webhook-path
这是我更新后的控制器代码:

    class WebhooksController < ApplicationController

      def receive
        token = params[:data][:token]

        response.headers["token"]= token
        render :text => token
      end

    end
class WebhooksControllertoken
结束
结束

目前还不清楚这个特定API应该如何返回令牌,我也不确定是应该在头中返回响应,还是返回JSON或文本。呈现文本并在标题中设置标记为我解决了这个问题

你能让它以params[:data]的形式输入,然后在事实发生后提取令牌吗?@txdavidtx我也尝试过,但没有成功。@txdavidtx经过更多测试后,你对我的操作方式是正确的。除此之外,我还需要尝试一些事情,比如如何在响应中返回令牌,这就是我最初认为它不起作用的原因。谢谢太棒了,很高兴它成功了。