Forum Discussion

mihai_lita's avatar
mihai_lita
New Contributor
6 years ago

Transfer property to aplication/x-www-urlencoded request

Hi,

 

I've created a test suite which contains a test case.

1. I am calling an API that returns in it's response a clientId. eg. /api/v1/getClients

2. I am retrieving that clientId and use it to call another API, e.g /api/v1/client/{clientId}

3. The API from point 2 is returning accountNumber

4. I am trying to call another API (eg. api/v1/client/getAccount ) where I need to pass the accountNumber in the payload of the request, but the payload needs to be in application/x-www-form-urlencoded format : accountNumber=abc123&currency=USD&country=US

I am failing in transfering the retrieved accountNumber into the payload. 

Any ideas?


Thank you!

 

  • I managed to get it working with the following solution

    JSON RESPONSE FROM WHERE I NEED DATA

    {

    "clientId" : "1234556",

    "accountNumber" : "abc123"

    }

    Groovy script I used:

     

    def response = new groovy.json.JsonSlurper().parseText(context.response)
    
    log.info "clientId is: ${response.clientId}"
    log.info "accountNumber is: ${response.accountNumber}"
    
    context.testCase.setPropertyValue('CLIENT_ID', response.clientId.toString())
    context.testCase.setPropertyValue('ACCOUNT_NUMBER',  response.accountNumber.toString())

    And the in the post request I did the following

    Content-Type : application/x-www-form-urlencoded

    Body: 

    clientid=${#TestCase#CLIENT_ID}&accnt_number=${#TestCase#ACCOUNT_NUMBER}

     

    Thank you!

     

    • mihai_lita's avatar
      mihai_lita
      New Contributor

      I managed to get it working with the following solution

      JSON RESPONSE FROM WHERE I NEED DATA

      {

      "clientId" : "1234556",

      "accountNumber" : "abc123"

      }

      Groovy script I used:

       

      def response = new groovy.json.JsonSlurper().parseText(context.response)
      
      log.info "clientId is: ${response.clientId}"
      log.info "accountNumber is: ${response.accountNumber}"
      
      context.testCase.setPropertyValue('CLIENT_ID', response.clientId.toString())
      context.testCase.setPropertyValue('ACCOUNT_NUMBER',  response.accountNumber.toString())

      And the in the post request I did the following

      Content-Type : application/x-www-form-urlencoded

      Body: 

      clientid=${#TestCase#CLIENT_ID}&accnt_number=${#TestCase#ACCOUNT_NUMBER}

       

      Thank you!