英文:
How to get last n characters from a form request field in wiremock response template?
问题
In the response body, you want to extract the last 4 characters from the card[number]
field in the request body. You can achieve this using Wiremock's response templating feature.
Here's how you can do it:
"last4": "{{request.body.request.query.card[number].substring(12)}}"
This will extract the last 4 characters of the card[number]
field in the request body.
Please note that the exact syntax might depend on your specific Wiremock configuration and templating setup.
英文:
I have a wiremock stub like below
{
"id":"86c2bb75-6de5-48aa-9c14-3cfc2483d12b",
"name":"v1_payment_methods",
"request":{
"url":"/stripe/v1/payment_methods",
"method":"POST",
"bodyPatterns":[
{
"equalTo":"type=card&card[cvc]=890&card[number]=4242424242424242&card[exp_month]=1&card[exp_year]=2043",
"caseInsensitive":false
}
]
},
"response":{
"status":200,
"body":"{\n \"id\": \"pm_{{randomValue length=24 type='ALPHANUMERIC'}}\",\n \"object\": \"payment_method\",\n \"billing_details\": {\n \"address\": {\n \"city\": null,\n \"country\": null,\n \"line1\": null,\n \"line2\": null,\n \"postal_code\": null,\n \"state\": null\n },\n \"email\": null,\n \"name\": null,\n \"phone\": null\n },\n \"card\": {\n \"brand\": \"visa\",\n \"checks\": {\n \"address_line1_check\": null,\n \"address_postal_code_check\": null,\n \"cvc_check\": \"unchecked\"\n },\n \"country\": \"US\",\n \"exp_month\": {{formData request.body 'form' urlDecode=true}}{{lookup form 'card[exp_month]'}},\n \"exp_year\": {{formData request.body 'form' urlDecode=true}}{{lookup form 'card[exp_year]'}},\n \"fingerprint\": \"{{randomValue length=16 type='ALPHANUMERIC'}}\",\n \"funding\": \"credit\",\n \"generated_from\": null,\n {\"last4\": \"4242\",\n \"networks\": {\n \"available\": [\n \"visa\"\n ],\n \"preferred\": null\n },\n \"three_d_secure_usage\": {\n \"supported\": true\n },\n \"wallet\": null\n },\n \"created\": {{now format='unix'}},\n \"customer\": null,\n \"livemode\": false,\n \"metadata\": {},\n \"type\": \"{{formData request.body 'form' urlDecode=true}}{{form.type}}\"\n}",
"transformers":[
"response-template"
]
},
"uuid":"86c2bb75-6de5-48aa-9c14-3cfc2483d12b",
"persistent":true,
"insertionIndex":7
}
In the response body last4
I want to get the last 4 characters from card[number]
field in request body
. Can someone let me know how can I do this?
Wiremock version
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8-standalone</artifactId>
<version>2.35.0</version>
</dependency>
答案1
得分: 0
使用Handlebars的substring
函数,您可以将您的last4
响应嵌套如下:
...\"last4\": "\{{formData request.body 'form' urlDecode=true}}{{substring (lookup form 'card[number]') 12}}\"...
substring
是一个Handlebars字符串辅助函数,用于在给定字符串中查找一个字符串,从指定字符的索引开始(在这种情况下是第13个字符),一直到第二个数字的索引(如果有的话,否则,如本例中,一直到字符串的末尾)。
将其与lookup
函数结合使用,通过用括号嵌套它,提供了substring
函数用于查找的值。
英文:
Using the substring
function from Handlebars, you can nest your last4
response as follows:
...\"last4\": "\{{formData request.body 'form' urlDecode=true}}{{substring (lookup form 'card[number]') 12}}\"...
substring
is a handlebars string helper function to find a string within a string, starting at the index of the character in a given string (in this case, the 13th character), going until the index of the second number, if given (otherwise, such as this case, going until the end of the string.)
Combining that with the lookup
function, by nesting it with parenthesis, provides the value that the substring
function uses for lookup.
答案2
得分: 0
"我决定使用 regexExtract
"
{{#assign 'last4'}}{{formData request.body 'form' urlDecode=true}}{{lookup form 'card[number]'}}{{/assign}}\"last4\": \"{{regexExtract last4 '.{4}$'}}\"
英文:
I decided to use regexExtract
{{#assign 'last4'}}{{formData request.body 'form' urlDecode=true}}{{lookup form 'card[number]'}}{{/assign}}\"last4\": \"{{regexExtract last4 '.{4}$'}}\"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论