Writing REST request and response data to a specified file.

2,378 views
Skip to first unread message

rman...@tibco.com

unread,
Nov 23, 2015, 12:14:33 AM11/23/15
to REST assured
Hi All,

Method 'log().all()' prints request data in console. I want to print this request data into a specified file. Also, I want to log the response for each request in the same file. So, the file should look like below:

Request:
Request method: GET
Request path: REST_URL
Proxy: <none>
Request params: <none>
Query params: <none>
Form params: <none>
Path params: <none>
Multiparts: <none>
Headers: Accept=*/*
Content-Type=application/xml; charset=ISO-8859-1
Cookies: <none>
Body: <none>

Response:
<?xml version="1.0" encoding="UTF-8"?>< ldap-container id="17" name="easyAs" description="REST Container" last-accessed="2015-11-23T03:57:15.334Z" active="true"><primary-ldap ldap-alias="easyAs" ldap-query="(cn=*)" resource-name-attributes="ou"/></ldap-container>


Can anyone please help me ????


Thanks,
Ranjeet

Johan Haleby

unread,
Nov 23, 2015, 2:42:29 AM11/23/15
to rest-a...@googlegroups.com
Hi,

Make use of LogConfig and set the default stream to write to disk. For example RestAssured.config = RestAssured.config().logConfig(logConfig().defaultStream(..)); You can find some inspiration here.

Regards,
/Johan

--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

rman...@tibco.com

unread,
Nov 25, 2015, 5:21:17 AM11/25/15
to REST assured
Hi Johan,

Thanks a lot for the precious help.

We have decided to use TestNG  which has method 'Reporter.log(String)'. It writes provided String in e-mail able report file(html file). 

Can you please tell me how can I extract the request details(please see below)  and put it in the String? As I can simply write 'Reporter.log(response.asSting())' to log response data.

Request method: GET
Request path:        http://serverip:port/baseurl/restcall
Proxy:                  <none>
Request params:   <none>
Query params:     <none>
Form params:      <none>
Path params:      <none>
Multiparts:               <none>
Headers:          Accept=*/*
                             Content-Type=application/xml; charset=ISO-8859-1
Cookies:                <none>
Body:                     <none>



Thanks,
Ranjeet

Johan Haleby

unread,
Nov 25, 2015, 6:51:27 AM11/25/15
to rest-a...@googlegroups.com
Hi, 

I think I already told you and even gave you an example (the test class I provided in the previous mail). Some of the tests actually write the log to a string.

Regards
/Johan

rman...@tibco.com

unread,
Nov 30, 2015, 12:01:51 AM11/30/15
to REST assured
Hi Johan,

I tried below code but in emailable-report.html I am getting just response printed not the request.

        final StringWriter writer = new StringWriter();
final PrintStream captor = new PrintStream(new WriterOutputStream(writer));

        /****************** Organization Model REST Calls ****************/

        @BeforeClass
   public void setupMethod(){
             RestAssured.config = config().logConfig(new LogConfig().enablePrettyPrinting(false));
  }

        /**
     * Use OrgModelService requests to get information about organization
   * models.
      */
    public Response listVersions() {

                Reporter.log("\nlistVersions");

                Response rs = given()
                          .config(config().logConfig(new LogConfig(captor, true))).log()
                         .everything().urlEncodingEnabled(false).when()
                         .get("REST_URL");

                Reporter.log("\n" + rs.asString());

                return rs;
     }

   @Test
  public void listVersionsTest() {

                Response rs = listVersions();
  }


Can you please let me know did I miss something?


Thanks,
Ranjeet

rman...@tibco.com

unread,
Nov 30, 2015, 12:28:22 AM11/30/15
to REST assured
Hi Johan,

working fine after adding  "Reporter.log(writer.toString()+LINE_SEPARATOR);". Request is getting printed in the emailable-report.html but I doubt that LINE_SEPARATOR property is working. Please find printed request below:

listOrgModelVersions
Request method: GET Request path:       http://BASE_URL/orgmodel/versions Proxy:                     <none> Request params:  <none> Query params:    <none> Form params:     <none> Path params:     <none> Multiparts:              <none> Headers:         Accept=*/* Cookies:             <none> Body:                    <none>

Just want to print it the way it gets printed in the console.

Thanks,

Johan Haleby

unread,
Nov 30, 2015, 2:01:23 AM11/30/15
to rest-a...@googlegroups.com
I don't know why that is happening, you probably need to ask google. It doubt that it has anything to do with REST Assured though.
Reply all
Reply to author
Forward
0 new messages