Making POST, PUT, and DELETE requests is a crucial web scraping and web testing technique. Still, this functionality is not included in Puppeteer's API as a separate function.
Let's check out the workaround for this situation and create a helper function to fix this out.
POST request is one of several available HTTP request types. By design, this method is used to send data to a web server for processing and possible storage.
POST requests usage is one of the main ways to send form data during login or registration. It is also one of the ways to send any data to the web server.
Earlier, one of the main implementation patterns of login or registration was to send the form data with the required authorization parameters via POST request and get the protected content as a response to this request (along with cookies to avoid re-entering the authentication and authorization data).
Nowadays SPAs (Single Page Applications) also use POST requests to send data to API, but such requests usually return only necessary data to update web page and not the whole page.
Thus, many sites use POST requests for client-server communication and this requires the ability of sending POST requests while web scraping.
Unfortunately, Puppeteer developers haven't introduced the native way of making requests other than GET, but it's not a big deal for us to create a workaround.
The idea behind our approach is quite simple - we need to change the request type while opening the page, so we can send POST data along with opening a page.
To do that, we have to intercept the request using
We're going to use HTTPBin which can help us with our solution testing.
Let's check out the simple JS snippet which just opens HTTPBin's POST endpoint:
The result is, definitely, not what we're trying to achieve:
So, let's add a request interception:
This time our POST request has been successfully executed:
Unfortunately, this code is only a proof-of-concept, not a complete solution, because any request made by the browser will be converted to a POST request.
Let's improve our code and make it production-ready.
The next big step of our HTTP requests Puppeteer journey is to create something reusable to avoid code duplication while making non-GET requests.
To achieve that, let's create a function
The usage of this function is straightforward and simple:
It is also possible to use this helper function with any available HTTP method and custom headers.
Having the ability to send any request type using Puppeteer allows web scraping specialists to extend their capabilities and improve their data crawlers' performance by skipping unnecessary steps like form filling.
As usual, we recommend you extend your web scraping knowledge using our articles:
- Get all text from the page using Puppeteer - how to extract all the text from a page using Puppeteer
Happy Web Scraping, and don't forget to cover your code with unit-tests 👷