首页 > 解决方案 > 配置 Karate Mock 以使用具有 JSON、XML 以外的内容类型的 API

问题描述

我需要使用 Karate Mock 来模拟一个 API,它接受以下 Content-Type 的消息并做出相应的响应

'Content-Type: application/x-www-form-urlencoded'

我尝试在功能文件中对其进行如下配置,但遇到了以下问题。我猜默认情况下它接受 JSON 作为内容类型,有没有办法配置模拟来读取像这样的自定义消息。

实际 API 调用:

curl -X POST -H "Content-Type: application/x-www-form-urlencoded"    -d "elem_value=generic_value&address="  http://myhost:9090/api/resource_path

模拟功能文件:

Scenario: pathMatches('/api/resource_path') && typeContains('urlencoded') 
        * def responseStatus = 200
        * def response = read ('/payload/Success.json')
        * print response

实际错误:

Caused by: <eval>:1 ReferenceError: "generic_value" is not defined
        at jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:57)
        at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:319)
        at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:291)
        at jdk.nashorn.internal.objects.Global.__noSuchProperty__(Global.java:1441)
        at jdk.nashorn.internal.scripts.Script$43$\^eval\_.:program(<eval>:1)
        at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:637)
        at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:494)
        at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:393)
        at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:449)

标签: karate

解决方案


这应该已在较新版本的空手道中修复,请尝试升级。

我认为它将在 0.9.2 - https://github.com/intuit/karate/issues/649


推荐阅读