Created
March 19, 2013 05:31
-
-
Save calvincorreli/5193914 to your computer and use it in GitHub Desktop.
Middleware to fix the problem with Sendgrid sending webhook POSTs with an incorrect application/json content type even though the content is actually a sequence of JSON structures separated by line breaks. Rails will try to parse the contents as a JSON document and fail. This middleware changes the content type to application/sendgrid-json, thus…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Billing::Application.configure do | |
config.middleware.insert_before "ActionDispatch::ParamsParser", SendgridWebhookMiddleware | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding : utf-8 -*- | |
require 'rack/utils' | |
class SendgridWebhookMiddleware | |
def initialize(app, url) | |
@app = app | |
@url = url | |
end | |
def call(env) | |
if env['PATH_INFO'] == url && env['REQUEST_METHOD'] == 'POST' | |
if env['CONTENT_TYPE'] =~ /^application\/json(;|$)/ | |
env['CONTENT_TYPE'] = env['CONTENT_TYPE'].gsub(/json/, 'sendgrid-json') | |
end | |
end | |
@app.call(env) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment