英文:
Wiremock response template to use json passed in as request query parameter
问题
以下是翻译的部分:
{
"response": {
"jsonBody": {
"logins": "{{request.query.Logins}}",
"jsonPathLogins": "{{jsonPath request.query.Logins '$'}}",
"jsonPathLogins0": "{{jsonPath request.query.Logins '$.[0]'}}",
"jsonPathQueryLogins": "{{jsonPath request.query '$.Logins'}}",
"jsonPathQueryLogins0": "{{jsonPath request.query '$.Logins.[0]'}}",
"loginsCut": "{{cut request.query.Logins.[0] '\"'}}",
"loginsCutQuot": "{{cut request.query.Logins.[0] '"'}}",
"loginsReplace": "{{replace request.query.Logins.[0] '\"' '*'}}",
"loginsReplaceQuot": "{{replace request.query.Logins.[0] '"' '*'}}"
}
}
}
注意:翻译中的双引号已经修复为正常的 JSON 双引号。
英文:
Provided with the following request:
http://myserver:8080/api/userinfo?Logins=%5B%22admin%40my.domain%22%5D
How would I get the first value in urlencoded-json array of values found in Logins
query parameter to output in Wiremock response templating?
Following are my attempts:
{
"response": {
"jsonBody": {
"logins": "{{request.query.Logins}}",
"jsonPathLogins": "{{jsonPath request.query.Logins '$'}}",
"jsonPathLogins0": "{{jsonPath request.query.Logins '$.[0]'}}",
"jsonPathQueryLogins": "{{jsonPath request.query '$.Logins'}}",
"jsonPathQueryLogins0": "{{jsonPath request.query '$.Logins.[0]'}}",
"loginsCut": "{{cut request.query.Logins.[0] '\"'}}",
"loginsCutQuot": "{{cut request.query.Logins.[0] '"'}}",
"loginsReplace": "{{replace request.query.Logins.[0] '\"' '*'}}",
"loginsReplaceQuot": "{{replace request.query.Logins.[0] '"' '*'}}"
}
}
}
and my response is always the same:
{
"logins": "["admin@my.domain"]",
"jsonPathLogins": "["admin@my.domain"]",
"jsonPathLogins0": "["admin@my.domain"]",
"jsonPathQueryLogins": "["admin@my.domain"]",
"jsonPathQueryLogins0": "["admin@my.domain"]",
"logins": "["admin@my.domain"]",
"loginsCut": "["admin@my.domain"]",
"loginsCutQuot": "["admin@my.domain"]",
"loginsReplace": "["admin@my.domain"]",
"loginsReplaceQuot": "["admin@my.domain"]"
}
What am I missing? How do I combine those methods to unwrap my value from that double JSON encoding (or somehow strip wrapping symbols)?
答案1
得分: 1
以下是翻译好的代码部分:
{
"response": {
"jsonBody": {
"logins": "{{replace(replace(replace(replace(urlEncode request.query.Logins decode=True) '%22' '') '%5B' '') '%5D' '') '%40' '@'}}"
}
}
}
{
"response": {
"jsonBody": {
"login": "{{regexExtract request.query.Logins '\\b(.+?@.+)\\b' 'parts'}}{{join parts ' '}}"
}
}
}
英文:
So far what I've come up with is the following wrapper:
{
"response": {
"jsonBody": {
"logins": "{{replace(replace (replace (replace (urlEncode request.query.Logins decode=True) '%22' '') '%5B' '') '%5D' '') '%40' '@'}}"
}
}
}
It looks awful and prone to edge cases, but work for some simple ones. I would gladly accept an answer which does not rely on replacing urlEncoded sequences directly the way I did it.
EDIT:
Here's another regex-based solution which works in my email-like case and looks a bit cleaner, but would fail in any other case:
{
"response": {
"jsonBody": {
"login": "{{regexExtract request.query.Logins '\b(.+?@.+)\b' 'parts'}}{{join parts ' '}}",
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论