Skip to content

Instantly share code, notes, and snippets.

@calvincorreli
Created March 19, 2013 05:31
Show Gist options
  • Save calvincorreli/5193914 to your computer and use it in GitHub Desktop.
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…
Billing::Application.configure do
config.middleware.insert_before "ActionDispatch::ParamsParser", SendgridWebhookMiddleware
end
# -*- 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