Ask a Question

Formatting data in a request

SOLVED
bklabel1
Occasional Contributor

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.

 

Thanks,

Kevin

Please give me a Kudo and mark if I answered your question fully. I'm kinda new here so this may not apply for a while. 🙂
3 REPLIES 3
ChrisAdams
Champion Level 2

Hi,

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.

 

Example script...

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;
HimanshuTayal
Community Hero

@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('-')[0] + ')'

desired_num = with_braces +'-'+ r.split('-')[1] +'-'+ r.split('-')[2]

log.info desired_num

Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal
bklabel1
Occasional Contributor

Himanshu,

I appreciate your explanation and sample of groovy code which is new to me.

Thanks,

Kevin

Please give me a Kudo and mark if I answered your question fully. I'm kinda new here so this may not apply for a while. 🙂
cancel
Showing results for 
Search instead for 
Did you mean: