同期的に GET / POST リクエストを送る

Prev Next

指定した API エンドポイント に対して GET / POST リクエストを送ります。

これらのコードはサンプルですので、ヘッダーやボディにはリクエストする API の仕様に沿って適切な値を設定してください。

指定した URL に対して GET リクエストを送る

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);

指定した URL に対して POST リクエストを送る

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);