将请求发布到普通路线,不使用API​​中间件

发布于 2025-01-26 00:56:35 字数 974 浏览 3 评论 0原文

在我的页面目录下方,我有一些路线(例如“/产品/详细信息”)。 我正在使用getServersideProps()使页面渲染服务器端。

如何将包含数据中包含数据的POST请求直接发送到此页面? 这个想法是我可以做这样的事情:

export async function getServerSideProps(postData) {
    return {
        props: {postData.body},
    }
}

我尝试了控制台记录“ PostData”。我可以看到正在发送邮政请求标题,但是请求正文丢失了。

谢谢

编辑: 我正在使用Postman进行发布,并且正在发送包含单个键的JSON类型的原始体系:值。但是正如我所说,该页面似乎没有收到已发布的数据。

这是我如何使用PuppeTeer将邮政请求发送到路由的代码段:

      const page = await puppeteerConnection.newPage();
      await page.setRequestInterception(true);
      await page.once('request', (request) => {
        let data = {
          'method': 'POST',
          'postData': JSON.stringify(jsonData),
          'headers': {
            ...request.headers(),
            'Content-Type': 'application/json'
          },
        };
        request.continue(data);
        page.setRequestInterception(false);
      });
      await page.goto('pathToNextJSRoute');

Below my pages directory i have a few routes (for example "/product/details").
I'm using getServerSideProps() to have the page render server side.

How can i send a POST request containing data in the body to this page directly?
The idea would be that i can do something like this:

export async function getServerSideProps(postData) {
    return {
        props: {postData.body},
    }
}

I've tried console logging "postData". I can see that the post request headers are being sent, but the request body is missing.

Thanks

Edit:
I'm doing the posting using Postman, and i'm sending a raw body of type JSON containing a single key:value. But as i said, the page doesn't seem to receive the posted data.

Here is a code snippet for how i'm sending a post request to a route using puppeteer:

      const page = await puppeteerConnection.newPage();
      await page.setRequestInterception(true);
      await page.once('request', (request) => {
        let data = {
          'method': 'POST',
          'postData': JSON.stringify(jsonData),
          'headers': {
            ...request.headers(),
            'Content-Type': 'application/json'
          },
        };
        request.continue(data);
        page.setRequestInterception(false);
      });
      await page.goto('pathToNextJSRoute');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我家小可爱 2025-02-02 00:56:35

getserversideprops()接受 a 上下文参数(您的示例中您命名为PostData)和该对象中的键之一(req )包含您要寻找的请求主体。但是,它是可读的字节数据流,因此您需要先将其转换为:

const streamToString = async (stream) => {
  if (stream) {
    const chunks = [];
    for await (const chunk of stream) {
      chunks.push(Buffer.from(chunk));
    }
    return Buffer.concat(chunks).toString("utf-8");
  }
  return null;
};

export async function getServerSideProps(context) {
  let data = null;
  if (context.req.method === "POST") {
    const body = await streamToString(context.req);
    data = JSON.parse(body);
  }

  console.log(data);

  return {
    props: { data },
  };
}

getServerSideProps() accepts a context parameter (which you've named postData in your example) and one of the keys in that object (req) contains the request body you're looking for. It arrives as a readable stream of byte data, though, so you'll need to convert it first:

const streamToString = async (stream) => {
  if (stream) {
    const chunks = [];
    for await (const chunk of stream) {
      chunks.push(Buffer.from(chunk));
    }
    return Buffer.concat(chunks).toString("utf-8");
  }
  return null;
};

export async function getServerSideProps(context) {
  let data = null;
  if (context.req.method === "POST") {
    const body = await streamToString(context.req);
    data = JSON.parse(body);
  }

  console.log(data);

  return {
    props: { data },
  };
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文