REST Assured 2.0 – Testing your REST services is easier than ever

REST Assured is a simple Java library for testing of REST services and a new major version has just been released. Not only is it easy to use and get started with but it’s also built to scale to more advanced use cases using detailed configuration, filters, specifications and the like. This version is simpler than ever mainly thanks to an updated syntax, given-when-then.

Old Syntax

Imagine that a REST service located at http://localhost:8080/lotto returns the following JSON document in its body:


The code below shows how to execute a GET request and validate that it’s ok (status code is 200), assert that the JSON document located in the response body has lotto id equal to 5 and that winner id’s are 23 and 54 in version 1.9.0 and earlier:

		queryParam("lottoId", 5).
		body("lotto.lottoId", equalTo(5)).
		body("", hasItems(23, 54)).

While this is quite simple it felt a bit awkward to many users to specify the assertions (expect) before the request has been sent. This is why REST Assured now supports an updated syntax of given-when-then.

New Syntax

With version 2.0 you now do like this instead:

		queryParam("lottoId", 5).
		body("lotto.lottoId", equalTo(5)).
		body("", hasItems(23, 54));

Which to most users feels more natural and familiar.

Extracting values

It’s also very easy to make requests and extract values out of the response with REST Assured. This is an example of how to return the winning-numbers list from the lotto JSON document:

List winningNumbers = get("/lotto?lottoId=5").path("lotto.winning-numbers");

But what if you want to make some assertions and then return the winning numbers? This is also very easy and looks like this:

List winningNumbers =
		queryParam("lottoId", 5).
		body("lotto.lottoId", equalTo(5)).
		body("", hasItems(23, 54)).

That’s it!


If you consider upgrading from an earlier version of REST Assured you can rest assured (!) that the old syntax will still work just as before. It’s just that the given-when-then syntax is as of 2.0 the recommended way of writing your tests. There are some benefits of using the older syntax though. The most prominent one is that REST Assured will present ALL failing assertions at the same time, something that is not possible with the updated syntax. If you’re interested in reading more about the new release refer to the release notes and usage guide.

This Post Has 11 Comments

  1. Dean Hayat

    Hello Johan,

    Just wanna ask something. I want to automate the REST services at work. Since I am new to REST assured and even to automation I am keen to know is there any step-by-step explanations to start with it. I couldn’t find any useful info online.

    I have tried with eclipse. Created a java project -> a package -> a class. Also included all the JAR files and here is my code. It’s failing and can you please help me? Thank you.

    import static com.jayway.restassured.RestAssured.*;
    import org.junit.Test;

    public class restSearchServices
    public void searchServices()
    get(“https://rest..(MY REST URL)./searchservice.svc/search”);

  2. Bhaskar

    I have a huge JSON file to be POST as payload of a rest api call for testing purposes. I tried something like :

    public void RestTest() throws Exception {
    File file = new File(“/Users/bmishra/Code_Center/stash/experiments/src/main/resources/Search.json”);
    String content = null;



    and get error as :

    java.lang.UnsupportedOperationException: Internal error: Can’t encode /Users/bmishra/Code_Center/stash/experiments/src/main/resources/Search.json to JSON.

    I can run by reading the file and passing the body as string and that works but I see i can directly pass the file object and this doesnt work.

  3. Zaheer Abbas

    I want to get the Status code of the site from Google Chrome via Selenium WebDriver using java. How can i accomplish my task..?

  4. Parveen


    We are exploring Restassure as a solution for our project, however I couldn’t find any solution for downloading CSV file using RestAssure api.

    Can you please help me with information?



    I am evaluating “rest-assured” to implement in my project for API testing for BDD.
    I have a scenario where before invoking any APIs (mostly we have CRUD Rest APIs written), I also need to set up some reference data.
    – So, I need to ensure the reference data specific test files runs first
    – Also, is it possible to define the sequence in which I would like to run the test cases.

  6. Sriram

    I am testing soap service with rest assured and my req and response is in XML. I am not able to extract the value of a particular XML tag in the same step where I do a validating. I am able to do it by hitting the web service separately once again (additional call) just to extract the response. If i Try to do it together by declaring a string variable and use extract method, then I am getting a error stating validatable response cannot be converted to string, something like this. Need help on this

  7. Chaityanya

    Can we use rest assured for testing violation of rest principles??

  8. Uday

    Is there a way that i can deploy the lotto webservices? I am pretty new to web service testing and API automation. To learn i can follow RestAssured documentation. But can i access or deploy this sample “Lotto” project? Or by chance do you know any public URLs which cover majority of Web Service operations?

  9. manish rai

    I am facing one issue ,when i am writting the below code:
    Response response_printReady=httpRequest.header(“Authorization”, authrization_token)


    I am getting the below issue:
    java.lang.IllegalArgumentException: Content-Type application/json is not valid when using multiparts, it must start with “multipart/”.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(

    In my webservice , i am not using any kind of uploading so that i have not used this multipart. With same configuration it is running in Postman but not by using RestAssured.
    Please help me to solve this issue.

Leave a Reply