Question about custom Plug parsers

Hey there,

I added have an endpoint that listens to webhooks, and for that endpoint I need the raw body, not the parsed one.

I had this before:

  plug(
    Plug.Parsers,
    parsers: [:urlencoded, :multipart, :json],
    pass: ["*/*"],
    json_decoder: Jason,
    length: 26_214_400
  )

and I clearly misunderstood pass, because I did this:

plug(Plug.Parsers,
    parsers: [:urlencoded, :multipart, :json],
    pass: ["stripe-webhooks/*"],
    body_reader: {XDEndpoint.CacheBodyReader, :read_body, []},
    json_decoder: Jason
  )

  plug(
    Plug.Parsers,
    parsers: [:urlencoded, :multipart, :json],
    pass: ["*/*"],
    json_decoder: Jason,
    length: 26_214_400
  )

so that on the stripe-wekhooks path it would pass but as I read about it now it is not what it does at all, so now all of my requests have the raw bodies put back into them, and that is a waste.
How could I limit the custom body reader to that specific endpoint? these are from my endpoint.ex file

If you open documentation for Plug.Parsers, you will see that the :pass option is doing something entirely different than you assume. It does not filter requests by URL/path, as you assumed, but instead by MIME type of the request (Content-Type request header I imagine).

So, no, I think you will not be able to do it this way.

I think you could leave the plug as it is, or move it into the router, so it parses the requests on paths you want, and add your own custom plug mounted in /stripe-webhooks/* in router, which would would preserve the raw request body. This seems to be what people are doing in such cases: https://github.com/phoenixframework/phoenix/issues/459#issuecomment-491288847.

Alternatively, you could probably hook up your own custom plug before the parsers in endpoint.ex.

hey, thanks for the answer,
yes, my approach here was incorrect,
for your 1-st i dont think i can do that in my case, because i have an endpoint app that reroutes to the actual app but at that point the parsing already happened,
the 2-nd that you mentioned is what i was trying to do, with my custom parser.