setExtraHTTPHeaders(headers)
此方法设置额外的 HTTP 头,这些头将随后续 HTTP 请求一起发送。
参数 | 类型 | 默认值 | 描述 |
---|---|---|---|
headers | Object<string, string> | 一个包含额外 HTTP 头的对象。所有头的值必须是字符串。 |
返回值
类型 | 描述 |
---|---|
Promise<void> | 一个 Promise,在头设置完成后解析。 |
示例
import { browser } from 'k6/browser';
export const options = {
scenarios: {
browser: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
},
};
export default async function () {
const page = await browser.newPage();
await page.setExtraHTTPHeaders({ foo: 'bar' });
const url = await page.goto('https://test.k6.io/browser.php');
console.log(url.request().headers().foo); // prints bar
}