Inconsistent behaviour with Stripe webhooks. Not passing Plug.Parsers randomly


I am experiencing something strange with Stripe webhooks.

We have a setup based on this article: Stripe Webhooks in Phoenix with Elixir Pattern Matching | Conner Fritz

We noticed some failing webhooks on the Stripe dashboard that say they got no response body (which should not happen, we send a response on every case from the relevant controller).

I started to look into it, and it looks like the request does not make it to the controller at all.

I added a plug to log when a step is passed in the endpoint.ex file.

  plug xxxWeb.StripeValidationPlug

  plug xxxWeb.LoggerPlug, msg: "passed stripe validation plug"

  plug Plug.Parsers,
    parsers: [:urlencoded, :multipart, :json],
    pass: ["*/*"],
    json_decoder: Phoenix.json_library(),
    # Max 1GB
    length: 1_000_000_000

  plug xxxWeb.LoggerPlug, msg: "passed Parsers plug"

Based on these logs it looks like we don’t make it past Plug.Parsers.

There are no errors, or any logs that would explain why this happens.

The only thing we see is that the request comes in and there is no response.

On other requests we see that the request came and response went:

Dec 16 09:57:11 AM  02:57:11.239 request_id=FzEmpgYZGousJoYADq7B [info] GET /healthz
Dec 16 09:57:11 AM  02:57:11.247 request_id=FzEmpgYZGousJoYADq7B [info] Sent 200 in 7ms

But for this we only see the incoming part:

Dec 16 09:57:07 AM  02:57:07.705 request_id=FzEmpTNucr4YJUoADq2B [info] POST /stripe_callback

I tried the same Stripe event, and sometimes it passes sometimes it doesn’t.

Any idea why this might be?
Or ideas to debug this?
Only happens on prod, seems rock solid locally/staging.


Seems like the request is crashing, so it’s strange that there are no errors or logs. Do you have an error reporting service set up?

If you can’t find the root cause, I’d suggest changing the approach and doing it the “recommended” way by implementing a custom body reader: I’ve successfully done this before for validating webhooks that required the raw request body. Please keep in mind that the example code needs some tweaking since Plug.Conn.read_body can return 3 different things.

Here’s my not-so-perfect implementation.

Thanks for the reply!

Yeah, we have error reporting.
I will make this change and see if it affects anything.
As for read_body, we seems to go past that point.

It seems like changing to this setup fixes the problem.


1 Like