Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can i get the raw body in api routes for webhook? #9541

Open
balramxyadav opened this issue Oct 12, 2024 · 13 comments
Open

How can i get the raw body in api routes for webhook? #9541

balramxyadav opened this issue Oct 12, 2024 · 13 comments

Comments

@balramxyadav
Copy link

I want to implement a webhook for a custom payment gateway, but no matter what I do, it doesn't work. I can never get the req.rawBody that the webhook requires. When I tried it with a normal Express server (sepreate from medusa), it worked perfectly, but for some reason, it's not working in Medusa. I tried the Express approach that didn't work, and then followed the routes approach Medusa recommends, and set up middleware as described here: https://docs.medusajs.com/development/api-routes/create#parse-webhook-body-parameters. But I still can't get the rawBody. Can someone help?

@olivermrbl
Copy link
Contributor

Please include a minimal reproduction of the issue, and we will attend to it.

@balramxyadav
Copy link
Author

balramxyadav commented Oct 12, 2024

Thanks for responding, quickly!

With the routes approach I tried:
/webhooks/test

import {
  MedusaRequest,
  MedusaResponse,
  ProductService,
} from "@medusajs/medusa";

export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
  res.status(200);
  console.log("Body", req.body);
  console.log("Raw Body:", req.rawBody);
};

and middleware as:
/middlewares.ts

import {
  MedusaNextFunction,
  MedusaRequest,
  MedusaResponse,
  MiddlewaresConfig,
} from "@medusajs/medusa";
import { raw } from "body-parser";

export const config: MiddlewaresConfig = {
  routes: [
    {
      method: ["POST", "PUT"],
      matcher: "/webhooks/*",
      bodyParser: false,
      middlewares: [raw({ type: "application/json" })],
    },
  ],
};

But i got rawData as undefined. I want to get the rawData.

@olivermrbl
Copy link
Contributor

If you want to access the raw body as req.rawBody, you should set preserveRawBody: true in the middleware too. Otherwise, the raw body is assigned to the regular req.body.

It would look like this:

export const config: MiddlewaresConfig = {
  routes: [
    {
      method: ["POST", "PUT"],
      matcher: "/webhooks/*",
      bodyParser: { preserveRawBody: true },\
    },
  ],
};

I believe you can then omit raw(...) from the middlewares, but I'll have to test this later.

Can you give this a shot and let me know if it works?

@balramxyadav
Copy link
Author

I had tried it before, but i tried again but unfortunately, it doesn't work :/

Do you have another solution?

@olivermrbl
Copy link
Contributor

What payment provider is this?

@balramxyadav
Copy link
Author

I am using Cashfree Payment. I didn't find any working plugin for it.

@olivermrbl
Copy link
Contributor

I had tried it before, but i tried again but unfortunately, it doesn't work :/

Do you have another solution?

Can you let me know what you are seeing with preserveRawBody: true configured?

Also, if you want me to test this out, please provide a small reproduction in a repository.

@balramxyadav
Copy link
Author

@olivermrbl
Copy link
Contributor

Great, I'll check it out.

QQ: Is there a reason you haven't started with the preview version of Medusa 2.0? If you can, I would recommend that to avoid an upgrade in the near term.

@balramxyadav
Copy link
Author

It's because i already have built my entire website. I am working on the implementing the payment gateway. Is it production ready yet? I heard it goes production ready on 23rd or something.

Copy link
Contributor

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 3 days.

@github-actions github-actions bot added Stale and removed Stale labels Nov 14, 2024
Copy link
Contributor

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 3 days.

@github-actions github-actions bot added the Stale label Dec 21, 2024
@SalahAdDin
Copy link

@balramxyadav Did you get a solution for it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants