Forum Discussion

Mis3's avatar
Mis3
Frequent Contributor
4 years ago

How to output the HTTP response to a log file

This is another API I am working with.

I use the following to output the results to a log file.

def header = tStep.getTestRequest().response.responseHeaders
log.info " Seq="+i+" "+"="+data[1]+"|Result:"+header
report << "\n"+ sdf.format(log_date) + " Seq=" + i + " "+data[1]+" Result:"+header

 

The API response does not provide too much information.  I can tell if it is working based on the HTTP status.  How do I output only HTTP/1.1 XXX to the report file (XXX is the result of the API)?

Also, I noticed that every time I run the Groovy after certain time of inactivity, I would get the Set-Cookie portion as well in the response (not sure why).

 

Thanks.

 

 

HTTP/1.1 204
Set-Cookie: JSESSIONID=26EEF6753526AAD776AB09823B11598B; Path=/rem; Secure; HttpOnly
Date: Wed, 15 Sep 2021 14:56:35 GMT

Header=  [Set-Cookie:[JSESSIONID=34C6105FD8CB3879790C36F253C70B45; Path=/rem; Secure; HttpOnly], #status#:[HTTP/1.1 204 ], Date:[Wed, 15 Sep 2021 14:59:27 GMT]]

 

HTTP/1.1 204
Date: Wed, 15 Sep 2021 14:54:55 GMT

Header= [#status#:[HTTP/1.1 204 ], Date:[Wed, 15 Sep 2021 15:00:47 GMT]]

 

HTTP/1.1 404
Set-Cookie: JSESSIONID=BBA3931D0CEA90EA0EBDCE444AF878FE; Path=/rem; Secure; HttpOnly
Content-Type: application/xml
Content-Length: 280
Date: Wed, 15 Sep 2021 14:31:00 GMT

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><error><type>EntityNotFound</type><message>Subscriber data could not be found for IMS user identity &quot;sip:+14313140097@ims.mnc130.mcc302.3gppnetwork.org&quot; and selection key &quot;Metaswitch::::&quot;</message></error>

Headerr= [#status#:[HTTP/1.1 404 ], Content-Length:[280], Date:[Wed, 15 Sep 2021 15:02:55 GMT], Content-Type:[application/xml]]

  • It is working now after some trial-and-errors.

    I replaced the def statement by String statement and the lines below did remove the square brackets.  

     

    String MESSAGE_TAS = tStep.getTestRequest().response.responseHeaders.'#status#'
    MESSAGE_TAS = MESSAGE_TAS.replaceAll("[\\[\\](){}]","")    // remove square brackets
    log.info " Seq="+i+" "+data[1]+",TAS-Put Result: "+MESSAGE_TAS

     

    Log info= Fri Sep 17 11:10:18 EDT 2021:INFO: Seq=5 14313140097,TAS-Put Result: HTTP/1.1 204

  • richie's avatar
    richie
    Community Hero
    Hey Mis3,

    The http response status code value you want is held in the #status# header. So you just need to extract that from the headers list.

    Re: the Set-cookie header. The set-cookie header is the initial header received when you hit a specific domain. Any subsequent calls to that endpoint will include a 'Cookie' header, one cookie header for each semi colon separated value in the original responses Set-cookie' header. The use of set-cookie/cookie headers is to enable the maintain the http session for all requests hitting the same endpoint. However http sessions expire. This is whats happening here. The session is expiring so the request generates the set-cookie header with an updated jsessionid value.

    Or at least i think thats how it works!

    Ta

    Rich
    • Mis3's avatar
      Mis3
      Frequent Contributor

      Thanks.   I tried this and it worked:

      def MESSAGE1 = tStep.getTestRequest().response.responseHeaders.'#status#'

      log.info " Seq="+i+" "+data[0] + " " + msisdn_1 + " " + MESSAGE1

       

      MESSAGE1 is now [HTTP/1.1 404 ].  Not sure where did these square brackets come from?   How do I get rid of them?

       

       

      • richie's avatar
        richie
        Community Hero
        Hey Mis3,

        Im on my phone so i cant double check. Can you 'log.info' MESSAGE1 variable immediately after youve declared it please?

        I want to see what it is before you start concatenating the other values together.

        Also, i thought you just wanted the status code. I didnt think you wanted the http version number as well.

        We can strip off the square brackets and the HTTP/1.1 if you need, but we need to do it BEFORE you start concatenating the string.

        Ta

        Rich