TS/playwright - 如何使用 x-www-form-urlencoded 正文发布 api 请求?

kiw*_*iwi 6 javascript typescript x-www-form-urlencoded playwright playwright-test

我需要使用 x-www-form-urlencoded 主体创建剧作家 API 请求:来自邮递员的示例: 工作邮递员请求示例

我正在尝试这样做:

  async getNewApiAccesToken({request})
    {
        const postResponse = await request.post("https://login.microsoftonline.com//token",{
            ignoreHTTPSErrors: true,
            FormData: {
                'client_id': 'xyz',
                'client_secret': 'xyz',
                'grant_type': 'client_credentials',
                'scope': 'api://xyz'
            }
        })
        console.log(await postResponse.json());
        return postResponse;
Run Code Online (Sandbox Code Playgroud)

但它不起作用:/你能告诉我如何在剧作家中撰写这种请求吗?

kiw*_*iwi 8

好的,我找到了解决方案!

 async getNewApiAccesToken({request})
    {
        const formData = new URLSearchParams();
        formData.append('grant_type', 'client_credentials');
        formData.append('client_secret', '8xyz');
        formData.append('client_id', 'xyz');
        formData.append('scope', 'api://xyz/.default');
        const postResponse = await request.post("https://login.microsoftonline.com/9xyz/v2.0/token",{
            ignoreHTTPSErrors: true,
            headers:{
                'Content-Type': 'application/x-www-form-urlencoded'
              },    
            data: formData.toString()  
        })
        return postResponse;
        
    };
Run Code Online (Sandbox Code Playgroud)