cancel
Showing results for 
Search instead for 
Did you mean: 

Formatting data in a request

SOLVED
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. 🙂
1 ACCEPTED SOLUTION

Accepted Solutions
Community Hero

Re: Formatting data in a request

@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

View solution in original post

3 REPLIES 3
Contributor

Re: Formatting data in a request

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

Re: Formatting data in a request

@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

View solution in original post

Occasional Contributor

Re: Formatting data in a request

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. 🙂
New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors