如何在Playwright中发送带x-www-form-urlencoded体的POST API请求?
在Playwright中构造x-www-form-urlencoded格式的API请求
你的代码无法正常运行的核心问题是请求体配置字段错误,同时URL存在冗余斜杠问题,修正方案如下:
- 替换请求体配置字段:将
FormData改为小写开头的form——Playwright的request.post方法中,form字段专门用于构造x-www-form-urlencoded类型的请求体,框架会自动匹配对应的Content-Type请求头。 - 修正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
相关产品推荐
相关产品推荐

