Ask a Question

[TechCorner Challenge #5] How to Fix Data in the Request Body Before Sending

SOLVED
sonya_m
SmartBear Alumni (Retired)

[TechCorner Challenge #5] How to Fix Data in the Request Body Before Sending

Hi everyone!

 

The TechCorner challenge is going really well, you are adding so much value by posting scripts here. The ReadyAPI Community is very thankful to everyone participating!

 

We sometimes need to fix the request body before sending it. There could be multiple reasons for doing so, really. Today, we are challenging you to create a script for such a use case. People will be able to modify it according to their specific needs.

 

Your task is to create a script that would check data in the request body, find all the white spaces at the end of ID values and delete them before it is sent to the server.

DifficultyStar_Gold.pngStar_gray.pngStar_gray.png

 

JSON example:

 

{"items":[{
"name":"John",
"password":"12345 "},
{"name":"Mary",
"password":"qwerty"},
{"name":"Sonya",
"password":"0000 "}]
}

 

Let's goπŸš€ 

(No, my password is not "0000", just to be clear πŸ˜‚)


Sonya Mihaljova
Community and Education Specialist

2 REPLIES 2
HimanshuTayal
Community Hero

Task: Your task is to create a script that would check data in the request body, find all the white spaces at the end of ID values and delete them before it is sent to the server.

 

This is a solution created for [TechCorner Challenge #5]

 

@sonya_m :

 

This TechCorner challenge is really going great and we all are loving it. Thanks to the Smartbear team for bringing these challenges time to time which helps whole community. πŸ™‚

 

So, here come the solution of new challenge that is create a script that would check data in the request body, find all the white spaces at the end of ID values and delete them before it is send to the server : 

 

 

 

 

 

def req = '''{
  "items": [
    {
      "name": "John",
      "password": "12345 "
    },
    {
      "name": "Mary",
      "password": "qwerty"
    },
    {
      "name": "Sonya",
      "password": "0000 "
    }
  ]
}'''
import groovy.json.JsonSlurper;

def reqModified = new JsonSlurper().parseText(req)
reqModified.items.each{	
	it.each{
		it.value = it.value.trim()
	}	
}
log.info reqModified

 

 

 

 

 

Hope it fulfills the requirement πŸ™‚ 

 


Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" πŸ™‚ ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal
sonya_m
SmartBear Alumni (Retired)

@HimanshuTayal Thank you for the script! Works perfectly.

The challenge is going great, indeed, we will keep you busy πŸ™‚!

 

 


Sonya Mihaljova
Community and Education Specialist

cancel
Showing results forΒ 
Search instead forΒ 
Did you mean:Β