当文件处于--form格式时,如何使用空手道做出适当的多部分请求? [英] How to make proper multipart request using Karate when file is in --form?
问题描述
我需要在空手道中实施这样的请求
I need to implement such a request in Karate
curl --location --request POST 'http://test.env.com:8080/runner/runners' \
--header 'authorization: Bearer TOKEN' \
--header 'content-type: multipart/form-data; boundary=---011000010111000001101001' \
--form 'createRunnerRequest={
"name": "Test banner - 1",
"header": "Test banner - 1 header",
"body": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
"priority": "1000",
"button": {
"objectType": "Button"
},
"isPersonalized": true,
"startDate":"2020.01.01"
}' \
--form 'image=@/Users/me/Downloads/jpeg2000-home.jpg' \
--form 'ebrList='
所以,我的第一次尝试是对我所有的字段进行多次调用
So, my first attempt was to call just multipart for all my fields
Scenario: 000
Given url bannerServiceUrl + 'banners'
And header content-type = 'multipart/form-data; boundary=---011000010111000001101001'
And header Authorization = 'Bearer ' + token
And multipart file image = { read: 'tst800x400.jpg', contentType: 'image/jpeg'}
And multipart field createBannerRequest = read('personal-test-runner-1.json')
When method post
Then status 200
响应告知我,我没有必需的请求部分-图片.所以我在这里意识到,我需要在这里使用表格...所以我做到了.
Response informed me, that I have no required request part - image. So I have realized here, that I need to use form here... So I did.
Scenario: 000
Given url bannerServiceUrl + 'banners'
And header content-type = 'multipart/form-data; boundary=---011000010111000001101001'
And header Authorization = 'Bearer ' + token
And form field image = read('tst800x400.jpg')
And form field createBannerRequest = read('personal-test-runner-1.json')
When method post
Then status 200
现在我遇到一个错误,它表示不支持application/x-www-form-urlencoded; charset = UTF-8-内容类型.
And now I have an error, which says that application/x-www-form-urlencoded;charset=UTF-8 - is not supported Content Type.
好吧,我知道,当我们使用表单时,它具有默认的内容类型-application/x-www-form-urlencoded; charset = UTF-8
Well, I know, when we use form, it bocomes default content type - application/x-www-form-urlencoded;charset=UTF-8
那么,我该如何正确编写测试?
So, how could I write test right way?
推荐答案
我认为表单字段
不起作用,这是多部分的,因此请使用多部分文件
或 field
.不用担心 boundary
空手道会做到这一点.
I think form field
won't work, this is multipart, so use multipart file
or field
for everything. Don't worry about the boundary
Karate will do that part.
您能尝试这样的事情吗?
Can you try something like this:
* url 'https://httpbin.org/anything'
* header Authorization = 'Bearer foo'
* multipart file image = { read: 'test.pdf', contentType: 'image/jpeg' }
* multipart file createBannerRequest = { read: 'test.json', contentType: 'application/json' }
* method post
* status 200
这篇关于当文件处于--form格式时,如何使用空手道做出适当的多部分请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!