在 Playwright 中发送 post 请求时收到 422 错误 - 参数丢失或值为空:数据 您的意思是吗?动作属性套件格式

问题描述 投票:0回答:2

我正在尝试在剧作家测试中发送以下内容的帖子请求:

    const testSet = await request.post('https://url/sets.json', {
        data: {
            "type":"sets",

            "attributes":{
      
               "name":"DuaneDemo",
      
               "priority":"highest"
      
            },
            "instances":{

                "test-ids":[obj]
             }
        },
        headers: {
            'PTToken': 'token'
        }
    })
    const response = await (await testSet.json());

我收到以下错误:

422 在 Playwright 中发送发布请求时出错 - 参数丢失或值为空:数据 你的意思? 动作属性套件格式

当我在 Postman 中运行此发布请求时,它成功了。 这是我的邮递员身体:

{ "data": {
    "type":"sets",

    "attributes":{

        "name":"DuaneDemo",

        "priority":"highest"

    },
    "instances":{

        "test-ids":["9163455", "9163685"]
        }
    }
}

任何帮助将不胜感激。 我认为我的剧作家帖子中的格式错误,但我不知道为什么。

javascript api post playwright
2个回答
0
投票

正确的格式是:

            data: {
                data: {
                    "type": "sets",

                    "attributes": {

                        "name": "DuaneDemo",

                        "priority": "highest"

                    },
                    "instances": {

                        "test-ids": [obj]
                    }
                }
            },


0
投票

const obj = ["9163455", "9163685"];

const testSet = await request.post('https://url/sets.json', {
    data: {
        "type": "sets",
        "attributes": {
            "name": "DuaneDemo",
            "priority": "highest"
        },
        "instances": {
            "test-ids": obj
        }
    },
    headers: {
        'PTToken': 'token', 
        'Content-Type': 'application/json' 
    }
});

const response = await testSet.json();
console.log(response);
//console.log(JSON.stringify(response, null, 2);

正确的错误确实意味着您的请求数据中有错误,因此可能是 test-id:obj 中的错误 仅使用一个等待来获取响应 如果你无法得到回复或者它看起来是空的或者[] 使用 JSON.stringify(响应, null, 2); >>> 进入console.log();

`const obj = ["9163455", "9163685"];

const testSet =等待request.post('https://url/sets.json', { 数据: { “类型”:“集”, “属性”: { “名称”:“杜安演示”, “优先级”:“最高” }, “实例”:{ “测试 ID”:obj } }, 标题:{ 'PTToken': '令牌', '内容类型':'应用程序/json' } });

常量响应=等待testSet.json(); 控制台.log(响应); //console.log(JSON.stringify(response, null, 2)`;

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.