Forum Discussion

meldoobs's avatar
meldoobs
Occasional Contributor
12 years ago

Output raw request and response including headers to a file

Hi all,
I'm very new to using groovy scripts and have very little programming knowledge so i've been picking up bits of scripts from the forum and adapting them.
So far i have been able to output the REST requests and responses i'm interesting in to a file by using the following statements in a groovy script immediately following my test step (eg not a tear down script)

myFile.append( context.expand('${my test step name#Request}') ) and
myFile.append( context.expand('${my test step name#Response}') )

This is great in that it outputs the actual values rather than the parameter names in my xml.

But i want to go one stage further and output the whole raw xml responses, including the http return code and headers etc. eg. exactly what you would see if you clicked on the "raw" tab in the UI.

Like this...

HTTP/1.1 200 OK
Date: Wed, 06 Feb 2013 21:42:33 GMT
Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept
Accept-Ranges: none
Server: anyoldserver.com.au
Content-Type: application/xml
Transfer-Encoding: chunked
Connection: close

<?xml version="1.0" encoding="ISO-8859-1" ?>
<blahblahblah>
......
</blahblahblah>

Thanks in advance for any help.
  • M_McDonald's avatar
    M_McDonald
    Super Contributor
    How about this:

    myRequestStep = testRunner.testCase.getTestStepByName('my test step name')
    myFile.append(new String(myRequestStep.testRequest.messageExchange.rawRequestData))
    myFile.append(new String(myRequestStep.testRequest.messageExchange.rawResponseData))
    • HimanshuTayal's avatar
      HimanshuTayal
      Community Hero

      M_McDonald

       

      Thanks for the post, this works perfectly fine.. I was looking for the solution from past one week..thanks a ton :) .. Keep posting good work.

  • meldoobs's avatar
    meldoobs
    Occasional Contributor
    M Mac, you are a star. Thanks very much, that worked perfectly.