如何在 puppeteer 中发送 POST 请求? [英] how do POST request in puppeteer?
本文介绍了如何在 puppeteer 中发送 POST 请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
(async() => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://www.example.com/search');
const data = await page.content();
browser.close();
res.send(data);
})();
我执行此代码是为了发送 get
请求.我不明白我应该如何发送 post
请求?
I do this code for send get
request. I don't understand how I should send post
request?
推荐答案
正确的顺序"可能有点挑战.文档没有那么多示例……示例文件夹中的存储库中有一些有趣的项目,您绝对应该看看.
Getting the "order" right can be a bit of a challenge. Documentation doesn't have that many examples... there are some juicy items in the repository in the example folder that you should definitely take a look at.
https://github.com/GoogleChrome/puppeteer/tree/master/examples
这是例子;将以下内容放入异步块:
Here is the example; place the following into an async block:
// Create browser instance, and give it a first tab
const browser = await puppeteer.launch();
const page = await browser.newPage();
// Allows you to intercept a request; must appear before
// your first page.goto()
await page.setRequestInterception(true);
// Request intercept handler... will be triggered with
// each page.goto() statement
page.on('request', interceptedRequest => {
// Here, is where you change the request method and
// add your post data
var data = {
'method': 'POST',
'postData': 'paramFoo=valueBar¶mThis=valueThat'
};
// Request modified... finish sending!
interceptedRequest.continue(data);
});
// Navigate, trigger the intercept, and resolve the response
const response = await page.goto('https://www.example.com/search');
const responseBody = await response.text();
console.log(responseBody);
// Close the browser - done!
await browser.close();
这篇关于如何在 puppeteer 中发送 POST 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文