我正在尝试使用
curl
在 TestCafe 中重新创建一个 t.request()
命令。我不知道如何将文件作为多部分 PUT/POST 请求的一部分上传。
这是基本的 curl 命令:
curl --request POST \
--url '<url>' \
--header 'X-Risk-Token: <token>' \
--header 'Content-Type: multipart/form-data' \
--form 'someField=@/path/to/file.png'
我无法通过 TestCafe 弄清楚语法,或者是否支持它
t.request()
我试过:
t.request.post({ url: url, headers: headers, params: { someField: "@/path/to/file.png" } })
但这从服务器得到了 500 错误,而它与 curl 一起工作。
编辑: 基本的 testcafe 示例(不工作):
const filepath = "/Users/codambro/file.png";
const url = "https://www.postman-echo.com/post";
/**
* curl command (returns 200):
* curl -X POST -F image=@/Users/codambro/file.png -H "Content-Type: multipart/form-data" --url 'https://www.postman-echo.com/post'
*/
fixture("Request Test");
test("Request Test", async t => {
const resp = await t.request.post({
url: url,
headers: { "content-type": "multipart/form-data" },
params: { "image": `@${filepath}` },
});
// Returning 500
console.log(resp.status);
console.log(resp.body);
});