Sends a GET / POST request to the specified API endpoint.
Since these codes are samples, please set appropriate values for the header and body according to the specifications of the API to be requested.
Send GET request to a URL
const url = "API URL";
// Send GET request
const response = await page.request.get(url);
// Check status code
if (response.status() !== 200) {
throw new Error("Error " + response.status());
}
// Parse JSON and output log
const data = await response.json();
console.log(data);
Send POST request to a URL
const url = "API URL";
// Send POST request
const response = await page.request.post(url, {
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
data: "key=value", // Form data
});
// Check status code
if (response.status() !== 200) {
throw new Error("Error " + response.status());
}
// Parse JSON and output log
const data = await response.json();
console.log(data);