Forum Discussion

sonya_m's avatar
sonya_m
SmartBear Alumni (Retired)
4 years ago
Solved

[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.

Difficulty

 

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 ðŸ˜‚)

  • 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 🙂 

     

2 Replies

  • 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 🙂 

     

    • sonya_m's avatar
      sonya_m
      SmartBear Alumni (Retired)

      HimanshuTayal Thank you for the script! Works perfectly.

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