Forum Discussion

liki7411's avatar
liki7411
Occasional Contributor
3 years ago
Solved

Trim the node value

I am using  a POST method in ReadyAPI 

the response is as follows,

 

<target_URL>: "http:/<servername>:port/k?key=abcdfee=

<httpmethod>GET</httpmethod>
<targetpayload null="true"/>
<isKBANeeded>true</isKBANeeded>

 

Issue 1: Unable to trim only the key value(in red) in target_URL

Issue 2: How to use the fetched key as parameter in other API's as header?

Steps performed: -

I need the key value (in red) from target_URL to pass as parameter to the next  API. Created the propertyTransfer and assigned the value to a variable.

But unable to use the value in header for other API.

  • Hi,

     

    What you're asking for is achievable.

     

    Firstly, how to extract the key.  Below is an example groovy script to tease out that value....

    def responseString = '<target_URL>: "http:/<servername>:port/k?key=abcdfee=' +
    	'<httpmethod>GET</httpmethod>' +
    	'<targetpayload null="true"/>' +
    	'<isKBANeeded>true</isKBANeeded>';
    
    def keyString = "key=";
    
    log.info(responseString);
    
    def indexOfKey = responseString.indexOf(keyString);
    def indexEndOfKey =  responseString.indexOf("=", indexOfKey + keyString.length());
    log.info("Key Start " + indexOfKey + ".  Key end " + indexEndOfKey);
    
    def key = responseString.substring(indexOfKey, indexEndOfKey);
    log.info(key);
    
    key = key.replace(keyString, "");
    log.info(key);
    
    return key;

     

    So, after the first call that returns the 'key', create a Groovy Script step called 'GetKey' and use the above example to read the response from the first call.  You will obviously have to tailor this to read from your first call.

     

    If your second API call is in the same test, then don't bother with property transfers etc.

     

    Simply pull in the value from the Groovy script and squirt it into the hear.  Go your second API call and click on the Headers tab.  Add Key ( or whatever it needs to be) and in value, you call the Groovy script...

     

    That dollar notation is great.  The above screenshot says 'call step GetKey and get the result value'.  When you run your test, ReadyAPI will substiture this for the actual value.

    If something isn't quite right, use the Raw tab to see the payload (with substituted variables) that ReadyAPI actually sent.

     

     

2 Replies

  • ChrisAdams's avatar
    ChrisAdams
    Champion Level 3

    Hi,

     

    What you're asking for is achievable.

     

    Firstly, how to extract the key.  Below is an example groovy script to tease out that value....

    def responseString = '<target_URL>: "http:/<servername>:port/k?key=abcdfee=' +
    	'<httpmethod>GET</httpmethod>' +
    	'<targetpayload null="true"/>' +
    	'<isKBANeeded>true</isKBANeeded>';
    
    def keyString = "key=";
    
    log.info(responseString);
    
    def indexOfKey = responseString.indexOf(keyString);
    def indexEndOfKey =  responseString.indexOf("=", indexOfKey + keyString.length());
    log.info("Key Start " + indexOfKey + ".  Key end " + indexEndOfKey);
    
    def key = responseString.substring(indexOfKey, indexEndOfKey);
    log.info(key);
    
    key = key.replace(keyString, "");
    log.info(key);
    
    return key;

     

    So, after the first call that returns the 'key', create a Groovy Script step called 'GetKey' and use the above example to read the response from the first call.  You will obviously have to tailor this to read from your first call.

     

    If your second API call is in the same test, then don't bother with property transfers etc.

     

    Simply pull in the value from the Groovy script and squirt it into the hear.  Go your second API call and click on the Headers tab.  Add Key ( or whatever it needs to be) and in value, you call the Groovy script...

     

    That dollar notation is great.  The above screenshot says 'call step GetKey and get the result value'.  When you run your test, ReadyAPI will substiture this for the actual value.

    If something isn't quite right, use the Raw tab to see the payload (with substituted variables) that ReadyAPI actually sent.

     

     

    • liki7411's avatar
      liki7411
      Occasional Contributor

      Thanks a lot !!