2014-03-27 41 views
1

我試圖找到最好的地方做到這一點。我中間件是這樣的:如何攔截並重寫受損的JSON調用與中間件

class Fixer 
    def initialize app 
    @app = app 
    end 

    def call env 
    if env["HTTP_ORIGIN"] == "https://where_i_expect_it_to_come_from.com/" 
    env['rack.input'] = StringIO.new('{"yo":"momma"}') # <-- But this info is not actually written before the call is passed! 
    end 

    env['rack.input'].rewind 
    status, headers, response = @app.call env 
    return [status, headers, response] 

    end 
end 

Rails.application.config.middleware.insert_before ActionDispatch::ParamsParser, Fixer 

看來,即使我在這裏改寫呼叫時,信息實際上並不正確改寫。任何想法如何讓我的內容在泡沫之前寫入?

回答

1

我覺得這個問題是因爲這裏的一樣:https://stackoverflow.com/questions/22712663/getting-a-routing-error-when-my-routes-are-clearly-marked這裏I have a third party service that I believe is sending me malformed JSON

從我的理解要解決一些傳入的請求數據。

作爲一個簡化的例子:

curl -X PUT -d "user[name]=something" http://localhost:3000/users/123 

我改變somethingphoet在定影劑

​​

並將其添加到application.rb

config.middleware.insert_before "ActionDispatch::ParamsParser", "Fixer" 

當我現在請求我的應用程序,我看到日誌

[localhost] [fdff4eb0-8387-41] Started PUT "https://stackoverflow.com/users/123" for 127.0.0.1 at 2014-03-30 13:16:02 -0400 
[localhost] [fdff4eb0-8387-41] Processing by UsersController#update as */* 
[localhost] [fdff4eb0-8387-41] Parameters: {"user"=>{"name"=>"phoet"}, "id"=>"123"} 
+0

不夠公平。這包括了這個部分,這在我的另一個問題中基本得到了回答。 – Trip

+0

@Trip我可能會誤解你的問題。你有什麼其他問題? – phoet

+0

無論我在我的Rack引擎中重寫的URL是什麼,Rails都無法找到該路線,除非我重寫路由到'/',而路由重定向到'/ quizzes'。 http://stackoverflow.com/questions/22712663/getting-a-routing-error-when-my-routes-are-clearly-marked – Trip