Formatting data in a request
My phone number is coming from an expanded expression in the format of 333-333-1234. I want to change it to (333)-333-1234 before using it in the POST.
I'm looking in documentation but I do not see how to do this.
Solved! Go to Solution.
You could used a Groovy Script test step to format the value prior to your API request step. Then, in your API request step, you can 'pull in' this formatted value.
def number = '333-333-1234' def firstHyphen = number.indexOf("-"); log.info("Insert a closed bracket at pos " + firstHyphen); def formattedNumber = number.substring(0, firstHyphen) + ")" + number.substring(firstHyphen); formattedNumber = "(" + formattedNumber; return formattedNumber;
@bklabel1 : There is no straight way to do this, you can achieve this by writing your groovy code.
You can use below code if input format will be as mentioned in question
def r = '333-333-1234' def with_braces = '(' + r.split('-') + ')' desired_num = with_braces +'-'+ r.split('-') +'-'+ r.split('-') log.info desired_num
Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓
Thanks and Regards,
I appreciate your explanation and sample of groovy code which is new to me.