菜单
开源

json()

返回响应体的 JSON 表示。如果响应体无法通过 JSON.parse 进行解析,则抛出错误。

返回值

类型描述
Promise<any>响应体的 JSON 表示。

示例

JavaScript
import { browser } from 'k6/browser';

export const options = {
  scenarios: {
    ui: {
      executor: 'shared-iterations',
      options: {
        browser: {
          type: 'chromium',
        },
      },
    },
  },
};

export default async function () {
  const page = await browser.newPage();

  try {
    const res = await page.goto('https://quickpizza.grafana.com/api/json?foo=bar');

    const json = await res.json();
    console.log(`json: ${JSON.stringify(json)}`); // json: {"foo": "bar"}
  } finally {
    await page.close();
  }
}