是否可以在 Strapi 上的定制端点上获取原始主体?

Rui*_*edo 7 strapi

我正在 Strapi 上构建一个自定义端点。对于这个端点,我需要拥有原始正文内容。是否可以从变量中获取它ctx

stripe : async(ctx) => {
    // Handle the event
    const sig = ctx.request.headers['stripe-signature']
    
    let event = null
    try {
      // ctx.request.body needs to be the original raw body
      event = stripe.webhooks.constructEvent(ctx.request.body,sig, endpointSecret)
    }catch (e) {
      ctx.badRequest(null,e)
      return
    }
Run Code Online (Sandbox Code Playgroud)

Abh*_*E H 8

创建一个中间件(/config/middleware.js)并将其更新为以下内容

module.exports = {
  settings: {
    cors: {
      enabled: true,
    },
    parser: {
      enabled: true,
      multipart: true,
      includeUnparsed: true,
    },
  },
};
Run Code Online (Sandbox Code Playgroud)

在控制器(/api/<model>/controllers/<model>.js)中:

const unparsed = require("koa-body/unparsed.js");
const unparsedBody = ctx.request.body[unparsed];
Run Code Online (Sandbox Code Playgroud)


Jim*_*RIE 0

我不确定是否了解您的需求。

ctx.request.body包含您请求的原始正文。

之后,如果您想将事件作为响应正文发送,您可以这样做。 ctx.body = event;

并在您的代码中发出警告。您编写了定义constforevent并分配了eventStrapi Webhook 的结果。您必须定义一个let变量。