how to see actual body sent from restassured
Asked Answered
F

4

10

I have created a jax-rs rest api and tested it with rest-assured. All tests are green. Now I am trying to create an html/js front end for it.

My problem is I do not know how my json objects should look like to be accepted by my rest api. Thanks to restassured/jax-rs I never handled request strings. I fill in objects and I get objects, (un)marshaling (json) is invisible.

Is there any way I can see (debug) what strings are created by rest-assured/java and sent over the "wire"?

Fourier answered 4/6, 2015 at 12:56 Comment(0)
W
18

If you want to log the request body you can do:

given().log().body(). ..

Or if you want to log the response body you can do:

.. .then().log().body(). ..

See documentation on logging for more details.

Weighted answered 10/10, 2015 at 6:56 Comment(1)
How to do it in non-BDD way?Microcircuit
D
0

I'm not a RestAssured used, so I can't answer your question directly, but here are some ideas that may help you out.

  • I don't know what serializer RestAssured uses under the hood, but Resteasy on Wildfly uses Jackson by default. I would get familiar with this library. For less trivial application, you may need to dig into its APIs directly to get your desired results. Here is it's documentation. For your particular case you can do something as simple as

    ObjectMapper mapper = new ObjectMapper();
    String jsonString = mapper.writeValueAsString(yourObject);
    System.out.println(jsonString);
    

    This will print out the POJO in JSON format, based on your getters in the class. This is at the most basic level. If you don't already have Jackson as a dependency, you can add

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.4.0</version>
    </dependency>
    
  • A really good friend (tool) to have is cURL. It's a command line tool that allows you to make REST/HTTP (other protocols also) requests. Though for this particular case it wouldn't help, you could send a GET request to one your resources that serves up the same type that you accepted in your POST. That way, you can see the resulting JSON. This may be a little much at this point, but I would definitely look into this tool if you're going to be doing a lot of REST development.

  • You might also want to check out a Browser tool like [Postman for Chrome]

  • You should really get familiar with JSON format. Once you get familiar with it, and you start working with JSON framework, you'll notice that at a basic level, they all work similarly.

    Java Object == JSON Object ( {} )
    Java Collection/Array == JSON Array ( [] )
    Java fields/properties == JSON keys
    
    Getters are used for they keys and their values (for serialization)
    Setters are used for deserialization
    

    So for example you have this class

    public class Person {
        String name;
        List<Person> friends;
    
        public String getName() { return name; }
        public void setName(String name) { return name; }
        // Getter and Setter for friends
    }
    

    An instance of Person would produce the following JSON

    {
        "name" : "Peeskillet",
        "friends": [
            {
                "name": "Lebron James"
            },
            {
                "name": "Steph Curry"
            }
        ]
    }
    

It's actually pretty simple once you get the hang of it.

  • Oh and another thing you can do is add a logging filter on the server side as mentioned here.

As far as working with Javascript, there is a JSON.stringify(javascriptObject) that will serialize your Javacript objects to JSON strings. So generally, you can model your Javascript object like your Java objects.

Hope this helped.

Division answered 4/6, 2015 at 15:39 Comment(1)
thanks for this in depth answe! the first part helped most (i already added jackson dependency for test) - so i use you code example to convert the stuff.Fourier
G
0

In RestAssured just use this:

String body = resp.asString(); System.out.println(body);

Got answered 6/10, 2015 at 12:23 Comment(1)
I think author wanted solution for request, not responseUnaccompanied
M
0

Try this for Request Body

 RequestSpecification httpRequest = RestAssured.given().urlEncodingEnabled(true);
..
..
..    
System.out.println(" Body ==> "+ httpRequest.log().body());

And for response body:

System.out.println("Res Body ===>"+ response.getBody().prettyPrint());

Microcircuit answered 29/3, 2022 at 15:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.