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 som...
  • HimanshuTayal's avatar
    4 years ago

    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 🙂