当文件处于 --form 时如何使用 Karate 发出正确的多部分请求?
How to make proper multipart request using Karate when file is in --form?
我需要在空手道中实现这样的要求
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='
所以,我的第一次尝试是为我的所有字段调用 multipart
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
响应通知我,我没有必需的请求部分 - 图片。
所以我在这里意识到,我需要在这里使用表格...所以我做到了。
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 - 不支持内容类型。
嗯,我知道,当我们使用表单时,它会变成默认的内容类型 - application/x-www-form-urlencoded;charset=UTF-8
那么,我怎样才能正确地编写测试呢?
我认为 form field
行不通,这是多部分的,所以对所有内容都使用 multipart file
或 field
。不用担心 boundary
空手道会做到这一点。
你能试试这样吗:
* 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
我需要在空手道中实现这样的要求
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='
所以,我的第一次尝试是为我的所有字段调用 multipart
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
响应通知我,我没有必需的请求部分 - 图片。 所以我在这里意识到,我需要在这里使用表格...所以我做到了。
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 - 不支持内容类型。
嗯,我知道,当我们使用表单时,它会变成默认的内容类型 - application/x-www-form-urlencoded;charset=UTF-8
那么,我怎样才能正确地编写测试呢?
我认为 form field
行不通,这是多部分的,所以对所有内容都使用 multipart file
或 field
。不用担心 boundary
空手道会做到这一点。
你能试试这样吗:
* 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