You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Playwright中发送带x-www-form-urlencoded体的POST API请求?

在Playwright中构造x-www-form-urlencoded格式的API请求

你的代码无法正常运行的核心问题是请求体配置字段错误,同时URL存在冗余斜杠问题,修正方案如下:

  1. 替换请求体配置字段:将FormData改为小写开头的form——Playwright的request.post方法中,form字段专门用于构造x-www-form-urlencoded类型的请求体,框架会自动匹配对应的Content-Type请求头。
  2. 修正URL格式:把URL中的//token改为/token,同时补充缺失的租户ID路径段(微软Azure AD令牌请求的必要参数),避免路径解析错误。

修正后的完整代码:

async getNewApiAccesToken({request})
{
    const postResponse = await request.post("https://login.microsoftonline.com/{你的租户ID}/token",{
        ignoreHTTPSErrors: true,
        form: {
            'client_id': 'xyz',
            'client_secret': 'xyz',
            'grant_type': 'client_credentials',
            'scope': 'api://xyz'
        }
    })
    console.log(await postResponse.json());
    return postResponse;
}

注:请将代码中的{你的租户ID}替换为实际的Azure AD租户ID,否则请求会无法定位到正确的认证端点。

内容的提问来源于stack exchange,提问作者kiwiKiwiKiwi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 13:10:57