Arthur Xu
Arthur Xu

Reputation: 31

Send POST request in puppeteer

I am working with JS+Puppeteer and I'm trying to make a script that can add items to cart. The website will often crash due to high traffic, so instead of relying on clicking, I want the script to send the item info directly to the backend. Once it does that, it will continue to do things. The add-to-cart link is different from the website's link.

For example, the website is abc.shop, and the backend API address is abc.shop/api/... I want to send a JSON continuing the item data to the api address.

Is it possible to send a POST request using puppeteer to a different address, and if so, how?

Upvotes: 3

Views: 8387

Answers (1)

Shahriar Shojib
Shahriar Shojib

Reputation: 927

You can use page.evaluate to send a post request using fetch api.

await page.evaluate(() => {
  return fetch('url', {method: 'POST', body: 'test' }).then(res => res.json());
});

https://pptr.dev/#?product=Puppeteer&version=v3.3.0&show=api-pageevaluatepagefunction-args

Upvotes: 11

Related Questions