Karate框架请求Payload格式变更致400错误求助
问题
在Karate框架中执行API测试时,发送包含安全证书的Payload返回400错误,但Postman中请求正常。原因是Karate会自动美化Payload格式,添加空格后导致内部嵌套的转义JSON字符串失效。
使用的Payload如下:
{"data": {"initiation": {"creditor_account": {"account_type": "CUENTA_CORRIENTE","bank_id": "0031","account_schema": "BICECONNECT.PAYMENTS","identification": "90354","user_identification": {"name": "Abono","id_schema": "CLID","id": "963708","email": "algo@bice.cl"}},"debtor_account": {"account_type": "CUENTA_CORRIENTE","bank_id": "0028","account_schema": "BICECONNECT.PAYMENTS","identification": "01362364","user_identification": {"name": "Cargo","id_schema": "CLID","id": "44","email": "algo@bice.cl"}},"instructed_amount": {"amount": "901","currency": "CLP"},"sender": {"id_schema": "BICECONNECT.SENDER","id": "Shinka","transaction_id": "d58093ca-f9f5-ef-5143e2d41ba4","creation_date": "2022-11-04T18:02:14.574","callback_url": "https://postman-echo.com/post"},"transaction_details": {"transaction_type": "payout","transaction_subtype": "transferencia","transaction_route": "CCA","description": "Paciencia, el apuro no conduce a nada"}}},"attachments": {"original_message": {"protected": "<secret-string>","payload": "{\"document\":{\"header\":{\"message_id\":\"f9841bc5-d03c-4364-bcd8-ea0caf8b1c8b\",\"creation_date\":\"2022-11-04T18:02:14.574\",\"sender\":{\"fin_id_schema\":\"SHINKANSEN\",\"fin_id\":\"BUK\"},\"receiver\":{\"fin_id_schema\":\"BICECONNECT.SENDER\",\"fin_id\":\"Shinkansen\"}},\"transactions\":[{\"transaction_type\":\"payout\",\"transaction_id\":\"d58093ca-f9f5-4a70-8def-5143e2d41ba4\",\"currency\":\"CLP\",\"amount\":\"901\",\"execution_date\":\"2022-11-04T18:02:14.574\",\"description\":\"Paciencia, el apuro no conduce a nada\",\"debtor\":{\"name\":\"Cargo\",\"email\":\"sebastian.fuenzalida@bice.cl\",\"identification\":{\"id_schema\":\"CLID\",\"id\":\"8000000001\"},\"financial_institution\":{\"fin_id_schema\":\"BICECONNECT.PAYMENTS\",\"fin_id\":\"0028\"},\"account_type\":\"CUENTA_CORRIENTE\",\"account\":\"01362364\"},\"creditor\":{\"name\":\"Abono\",\"email\":\"rafael.cruz@bice.cl\",\"identification\":{\"id_schema\":\"CLID\",\"id\":\"967713708\"},\"financial_institution\":{\"fin_id_schema\":\"BICECONNECT.PAYMENTS\",\"fin_id\":\"0031\"},\"account_type\":\"CUENTA_CORRIENTE\",\"account\":\"4\"}}]}}","signature": "<signature-string>"}}}"
预期返回201响应,该Payload复制自其他API的文本格式响应。
解决方案
核心问题是Karate会将传入的JSON对象自动格式化(添加空格、换行),但Payload中attachments.original_message.payload是转义后的JSON字符串,格式化会破坏其结构,导致服务端解析失败。解决方法是直接发送原始文本内容,不让Karate解析JSON:
方法1:使用text关键字发送原始字符串
在Karate脚本中,将Payload作为原始字符串传递,用text关键字指定请求体,避免Karate解析和格式化:
Scenario: Send raw payload without formatting Given url "your-api-endpoint-url" And header Content-Type = "application/json" And text `{"data": {"initiation": {"creditor_account": {"account_type": "CUENTA_CORRIENTE","bank_id": "0031","account_schema": "BICECONNECT.PAYMENTS","identification": "90354","user_identification": {"name": "Abono","id_schema": "CLID","id": "963708","email": "algo@bice.cl"}},"debtor_account": {"account_type": "CUENTA_CORRIENTE","bank_id": "0028","account_schema": "BICECONNECT.PAYMENTS","identification": "01362364","user_identification": {"name": "Cargo","id_schema": "CLID","id": "44","email": "algo@bice.cl"}},"instructed_amount": {"amount": "901","currency": "CLP"},"sender": {"id_schema": "BICECONNECT.SENDER","id": "Shinka","transaction_id": "d58093ca-f9f5-ef-5143e2d41ba4","creation_date": "2022-11-04T18:02:14.574","callback_url": "https://postman-echo.com/post"},"transaction_details": {"transaction_type": "payout","transaction_subtype": "transferencia","transaction_route": "CCA","description": "Paciencia, el apuro no conduce a nada"}}},"attachments": {"original_message": {"protected": "<secret-string>","payload": "{\"document\":{\"header\":{\"message_id\":\"f9841bc5-d03c-4364-bcd8-ea0caf8b1c8b\",\"creation_date\":\"2022-11-04T18:02:14.574\",\"sender\":{\"fin_id_schema\":\"SHINKANSEN\",\"fin_id\":\"BUK\"},\"receiver\":{\"fin_id_schema\":\"BICECONNECT.SENDER\",\"fin_id\":\"Shinkansen\"}},\"transactions\":[{\"transaction_type\":\"payout\",\"transaction_id\":\"d58093ca-f9f5-4a70-8def-5143e2d41ba4\",\"currency\":\"CLP\",\"amount\":\"901\",\"execution_date\":\"2022-11-04T18:02:14.574\",\"description\":\"Paciencia, el apuro no conduce a nada\",\"debtor\":{\"name\":\"Cargo\",\"email\":\"sebastian.fuenzalida@bice.cl\",\"identification\":{\"id_schema\":\"CLID\",\"id\":\"8000000001\"},\"financial_institution\":{\"fin_id_schema\":\"BICECONNECT.PAYMENTS\",\"fin_id\":\"0028\"},\"account_type\":\"CUENTA_CORRIENTE\",\"account\":\"01362364\"},\"creditor\":{\"name\":\"Abono\",\"email\":\"rafael.cruz@bice.cl\",\"identification\":{\"id_schema\":\"CLID\",\"id\":\"967713708\"},\"financial_institution\":{\"fin_id_schema\":\"BICECONNECT.PAYMENTS\",\"fin_id\":\"0031\"},\"account_type\":\"CUENTA_CORRIENTE\",\"account\":\"4\"}}]}}","signature": "<signature-string>"}}}"` When method post Then status 201
方法2:读取外部原始JSON文件
将Payload保存为一个无格式的JSON文件(比如payload.json),确保文件内容是压缩后的原始字符串,没有多余空格和换行。然后在Karate脚本中读取并作为text发送:
Scenario: Read raw payload from file Given url "your-api-endpoint-url" And header Content-Type = "application/json" And text read('classpath:payload.json') When method post Then status 201
方法3:禁用Karate的JSON格式化
如果需要继续使用JSON对象语法,可以通过配置禁用Karate的自动格式化,但此方法需要确保嵌套的转义字符串不被修改:
* configure prettyPrint = false * configure charset = 'UTF-8'
之后再发送JSON对象,但这种方法不如前两种可靠,因为Karate仍可能对JSON结构进行解析调整,建议优先使用前两种方法。
内容的提问来源于stack exchange,提问作者Juan Carlos Fuentes
相关产品推荐
相关产品推荐

