将请求发布到普通路线,不使用API中间件
在我的页面目录下方,我有一些路线(例如“/产品/详细信息”)。 我正在使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
getserversideprops()
接受 a上下文
参数(您的示例中您命名为PostData
)和该对象中的键之一(req )包含您要寻找的请求主体。但是,它是可读的字节数据流,因此您需要先将其转换为:
getServerSideProps()
accepts acontext
parameter (which you've namedpostData
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: