替换空手道中的 Json 键
Replace Json key in Karate
我需要将 Json 发送到端点,但我需要用变量替换密钥。
我有这个代码
.....
* def idJson = response.id
Given path <mypath>
And headers {Authorization: '#(auth)'}
And request read('classpath:myjson.json')
.....
myjson.json 文件是
{
“一种”:
{
...
“b”:假的,
“c”:真
},
“d”:
{
'#(idJson)':
{
“钥匙”:[
.....
]
}
}
}
但是 json 中的值没有被替换。当我执行请求时,我看到 json 包含字符串 '#(idJson)' 而不是变量的值。知道如何解决这个问题吗?
谢谢
嵌入表达式不能用于修改 JSON 键。你必须使用这样的 JS 操作:
* def idJson = 'foo'
* def body = {}
* body[idJson] = 'bar'
* url 'https://httpbin.org/anything'
* request body
* method post
* status 200
* match response.json == { foo: 'bar' }
请注意 d
可以这样替换,这样您仍然可以读取文件并保持动态:
{ "d": "#(temp)" }
而temp
可以是JSON,在读取文件之前在你的特性中定义。
如果嫌麻烦,请贡献代码:)
我需要将 Json 发送到端点,但我需要用变量替换密钥。
我有这个代码
.....
* def idJson = response.id
Given path <mypath>
And headers {Authorization: '#(auth)'}
And request read('classpath:myjson.json')
.....
myjson.json 文件是
{ “一种”: { ... “b”:假的, “c”:真 }, “d”: { '#(idJson)': { “钥匙”:[ ..... ] } } }
但是 json 中的值没有被替换。当我执行请求时,我看到 json 包含字符串 '#(idJson)' 而不是变量的值。知道如何解决这个问题吗?
谢谢
嵌入表达式不能用于修改 JSON 键。你必须使用这样的 JS 操作:
* def idJson = 'foo'
* def body = {}
* body[idJson] = 'bar'
* url 'https://httpbin.org/anything'
* request body
* method post
* status 200
* match response.json == { foo: 'bar' }
请注意 d
可以这样替换,这样您仍然可以读取文件并保持动态:
{ "d": "#(temp)" }
而temp
可以是JSON,在读取文件之前在你的特性中定义。
如果嫌麻烦,请贡献代码:)