Forum Discussion

Murali7660's avatar
Murali7660
Occasional Visitor
4 years ago

How to record rest response to file using groovy script : soapui tool

How to record rest response to local directory files using groovy script. I tried files are creating but responses are not saving, how to do that for rest api requests? Pls help me guys.

Advance thanks. 

  • ChrisAdams's avatar
    ChrisAdams
    Champion Level 3

    Hi,

     

    Pretty straightforward.  Create a Groovy test step after the API call.  In the Groovy step, add....

     

    def myRequestStep = testRunner.testCase.getTestStepByName(['SOAP Request']);  // Substiture 'SOAP Request' with your step name.
    
    def rawRequest = new String(myRequestStep.testRequest.messageExchange.rawRequestData);
    def rawResponse = new String(myRequestStep.testRequest.messageExchange.rawResponseData);
    
    // Log the raw request and raw response
    log.info(rawRequest);
    log.info(rawResponse);
    
    // Create the file for the request and write to it.
    def requestFile = new File("/some path/request.txt");
    requestFile.write(rawRequest);
    
    // Create the file for the response and write to it.
    def responseFile = new File("/some path/response.txt");
    responseFile.write(rawResponse);
    
    // Note the .write() action overwrites anty pre-existing file.