Forum Discussion

Thijis_Bakker's avatar
Thijis_Bakker
Occasional Contributor
15 years ago

HowTo use the HTTP request of a RestTestStep in groovy?

Hi,

I have many RestTestSteps and would like to print out the HTTP request (as displayed on the 'Raw'-Tab) or just the URL sent by soapUI.

The following code just produces empty output:
log.info(messageExchange.getRequestContent())

I wonder how to do this.

Thanks for your help,

René
  • def String generateJunitErrorMessage(String errorMessage){
    errorMessage = 'Data Supplied Assertion Failures: \n' + errorMessage
    errorMessage = errorMessage + '\nRequest: \n'
    def requestHeaders = messageExchange.getRequestHeaders()
    for(request_header_key in requestHeaders.getKeys()){
    errorMessage = errorMessage + request_header_key + "=" + requestHeaders.get(request_header_key) + '\n'
    }
    errorMessage = errorMessage + messageExchange.getRequestContent() + '\n'
    errorMessage = errorMessage + '\nResponse: \n'
    def responseHeaders = messageExchange.getResponseHeaders()
    for(response_header_key in responseHeaders.getKeys()){
    errorMessage = errorMessage + response_header_key + "=" + responseHeaders.get(response_header_key) + '\n'
    }
    errorMessage = errorMessage + messageExchange.getResponseContent() + '\n'
    }//end of generateJunitErrorMessage
  • Thijis_Bakker's avatar
    Thijis_Bakker
    Occasional Contributor
    Thanks,

    but if I log this in a groovy script assertion for a REST Request Step I get the following output:
    Data Supplied Assertion Failures:
    null
    Request:
    Host=IP:PORT
    Accept-Encoding=gzip,deflate
    User-Agent=Jakarta Commons-HttpClient/3.1
    Accept=application/xml


    Response:
    ....

    As you can see there is just the host, and not the request/URL used to invoke the REST service.

    Regards,

    René
  • You should be able to use the messageExchange.getRawRequestData() method.
    This returns a byte[] with the contents from the Raw panel, which can be converted into a String:


    log.info new String( messageExchange.getRawRequestData() )


    Which prints out something like:


    Wed Aug 11 11:41:37 CEST 2010:INFO:GET http://localhost:8080/NewsSearchService/V1/madonna_xml.xml HTTP/1.1
    Accept-Encoding: gzip,deflate
    User-Agent: Jakarta Commons-HttpClient/3.1
    Host: localhost:8080


    Regards,
    Dain
    eviware.com
  • Thijis_Bakker's avatar
    Thijis_Bakker
    Occasional Contributor
    Thanks Dain, I will use messageExchange.getRawRequestData() for the moment.

    There is one thing which is still disturbing:
    messageExchange.getRawRequestData() does not work in all cases. For instance if you do not receive a response due to a network timeout the byte[] is empty.

    I think, there should be at least one possibility to get the request/url used within the test step in all cases.

    Regards,

    René
  • M_McDonald's avatar
    M_McDonald
    Super Contributor
    I have noticed the same thing - the Raw tab is empty if the request times out. It would still have been parsed if there was an attempt to send it, so it would be nice to see it regardless of the response (or no response!)
  • Good point, this is a more or less a bug related to the internal architecture.. I'll try to figure it out..

    regards,

    /Ole
    eviware.com