2017-09-09 7 views
7

https://requestb.inを使用すると、webhookがヘッダ+ JSONボディデータを正しく送信していることがわかります。しかし、サーバーにjson要求を送信すると、jsonを解析する際にエラーが発生します。webhookリクエストのJSONを解析します。

マイコントローラー(身体データを受信することはできません):

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 

エラー出力:

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' 
+0

? –

+0

@Зелёныйそれは 'JSON.parse(request.raw_post)'の出力です。 'JSON.parse(request.body.read)'の出力を追加しました – Taylor

+0

私のコメントをお読みですか? 'puts request.body.read'の出力を表示します。 –

答えて

0

私はレールブロックを考えます要求を受信する(https://stripe.com/docs/webhooks)を提供レールCSRF保護のeは、私がストライプウェブフックにを使用して、初心者だが、ウェブフックのための彼らのドキュメントには、次の操作を行うために私に助言:

あなたがRailsのを使用している場合は、 Django、または他のWebフレームワークでは、サイトは自動的にすべてのPOST要求にCSRFトークンが含まれているかどうかをチェックします。これは、サイト間の要求偽造の試みからユーザーを保護する重要なセキュリティ機能です。ただし、このセキュリティ対策によって、サイトが正当なウェブフックを処理できなくなる可能性もあります。その場合、webhooksルートをCSRF保護から免除する必要があります。 ouptputは `request.body.read`を置くこと

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

私はOPと同じ問題があります私はコントローラのprotect_from_forgery行を持っているので、解決策はどこか別の場所になければならないと思います。 – luissimo

+1

@luissimo上記のように、これは問題を解決しませんでした。私は 'protect_from_forgery:except =>:handle_post'と' skip_before_action:verify_authenticity_token、:only:= [:handle_post] 'の両方を使っています。 – Taylor

関連する問題