在空手道中的 POST 请求正文中发送文件

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

在空手道中,我在让请求按预期工作时遇到一些困难,我在 POST 请求正文中使用 .json 文件。我尝试将其定义为表单字段和多部分文件。我认为我没有正确执行此操作,因为它们都没有得到与 CURL 命令相同的结果。我删除了一些秘密变量,它们肯定可以在 Postman 的 CURL 命令上下文中工作。

curl --location 'https://{{QA_URL}}/v1/lists/10790' \
--header 'accept: application/json' \
--header 'x-key: {{KEY}}' \
--header 'x-secret: {{SECRET}}' \
--form 'file=@"/Downloads/tmp-test4.json"'

这是我的空手道场景,其中我执行 GET 调用来检索列表集合,然后;执行 POST 请求来更新我的特定列表 - 声明文件的尝试失败了几次。该文件位于我的读取步骤中引用的文件位置。感谢您的帮助!

Scenario: GET list public api tests
* def requestBody = read("file:src/test/java/json/User/Request/updateListDetails.json")
* set requestBody.notes = randomString

Given path '/v1' + '/lists/'
And header Content-Type = 'application/json'
And header x-key = Key
And header x-secret = Secret
When method GET
* def listId = response.lists[2].id

Given path '/v1' + '/lists/' + listId
And header accept = 'application/json'
And header Content-Type = 'multipart/form-data'
And header x-key = Key
And header x-secret = Secret
#And form field file = read("file:src/test/java/json/User/Request/updateListDetails.json")
#And multipart field file = { read: 'file:src/test/java/json/User/Request/updateListDetails.json', filename: 'updateListDetails.json', contentType: 'multipart/form-data' }
#And request requestBody
When method POST
Then status 200
file karate
1个回答
0
投票

谢谢您的建议Peter,下面是能够让它为我工作的代码。我对文件的声明太细化了,我只需要使用

multipart file
,现在它的工作方式就像curl 命令一样。

Scenario: GET list public api tests
* def requestBody = 
read("file:src/test/java/json/User/Request/updateListDetails.json")
* set requestBody.notes = randomString

Given path '/v1' + '/lists/'
And header Content-Type = 'application/json'
And header x-key = Key
And header x-secret = Secret
When method GET
* def listId = response.lists[2].id

Given path '/v1' + '/lists/' + listId
And header accept = 'application/json'
And header Content-Type = 'multipart/form-data'
And header x-key = Key
And header x-secret = Secret
And multipart file file = { read: 'file:src/test/java/json/User/Request/updateListDetails.json', filename: 'updateListDetails.json', contentType: 'multipart/form-data' }
When method POST
Then status 200
© www.soinside.com 2019 - 2024. All rights reserved.