How to validate Non-JSON response body using Karate

↘锁芯ラ 提交于 2019-12-24 06:31:10

问题


I am trying to validate an error response body of non-json format. I have tried using the schema as well as a keyword using contains to match. It is not working for me.

Actual Response response returned from API when multiple matches are observed in a post call:

{"error":{"text":SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'NewEmp' for key 'employee_name_unique'}}

My Karate Feature:

Given url 'http://dummy.restapiexample.com/api/v1/create'
def errResp = "error"  
And request {"name":"Hello1","salary":"123","age":"23"}
When method POST
Then status 200
Then match $ contains error #not working
Then match $ contains errResp #not working

Tried * match actual[0] == '#(^partSchema)' too from https://gist.github.com/ptrthomas/2a1e30bcb4d782279019b3d5c10b3ed1 . Not working


回答1:


Please understand type-conversions first: https://github.com/intuit/karate#type-conversion

Then convert the response to a string before you want to do string contains checks:

* string temp = response
* match temp contains 'error'


来源:https://stackoverflow.com/questions/57372941/how-to-validate-non-json-response-body-using-karate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!