因此,我使用 Karate 中的功能创建了一个动态 POST 请求。我手动测试了新创建的请求,它确实返回了 200。但是,当通过 Karate 运行请求时,我得到了 415 Unsupported media type。
发布请求:
{
"content": [
{
"assetId": "273108817",
"country" : "US",
"sourceSystemCode" : "GE_Clarify",
"serviceRequestCode" : "Karate-Insert: 157264280",
"serviceRequestTypeCode" : "servicerequestcorrective",
"serviceRequestStatusCode" : "Started",
"requester" : "Karate,DSL",
"problem" : "Submitted Using Karate",
"submissionTimestamp" : null,
"sourceUpdateDate": "2017-09-13T15:53:51.597Z",
"completionDate" : null,
"dueDate" : null,
"availabilityDate" : null,
"remotely" : "0",
"assetAvailability" : "Up",
"facilityCode" : "US_294629"
}
]
}
Karate 语法
场景:提交新的服务请求 POST:
def ServiceRequestPostTemplate = read('classpath:testsuite/testdata/ServiceRequestServiceTemplate.json') * 替换 ServiceRequestPostTemplate |代币 |值(value) | | Assets 编号 | Assets 表 Assets ID | |国家 |国家 | |来源系统代码 |来源系统代码 | |服务请求代码 | ' Karate 插入:' + NewServiceRequestCode |
给定路径 'serviceRequests/' 并请求ServiceRequestPostTemplate
- header Authorization = 'Bearer' + Token 当方法发布 然后状态200
错误信息在下面的附件中。请注意,我通过 Swagger UI 手动检查了请求,一切正常。我还在 Karate 中将它作为硬编码变量传递进来并且它工作正常。但是,使用“表格”功能似乎会改变媒体类型。
最佳答案
您还需要使用 header 关键字添加内容类型,如下所示
给定路径 'some/path'
并请求{ some: 'data' }
和标题 Accept = 'application/json'
方法发布时
然后状态200
希望这对您有所帮助。
关于rest - 在 Karate DSL 中,如何为有效的 POST 请求解析不受支持的媒体类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46839414/