Sending Your First GET Request
If you’ve successfully set up REST Assured in your Java project, it’s time to write your very first API automation test. In this tutorial, you’ll learn how to send a REST Assured GET request, validate the response, and understand the given(), when(), and then() syntax that forms the foundation of almost every REST Assured test.
By the end of this guide, you’ll be able to automate simple GET APIs with confidence.
Prerequisites
Before proceeding, make sure you have:
- Java installed
- Maven project created
- REST Assured dependency added
- An IDE such as IntelliJ IDEA or Eclipse
If you haven’t completed the setup yet, read the previous article:
How to Set Up REST Assured in Java Using Maven
What Is a GET Request?
A GET request retrieves data from a server without modifying it.
For example:
GET https://reqres.in/api/users/2
This endpoint returns the details of user ID 2.
Unlike POST or PUT requests, GET requests are considered safe because they only retrieve information.
Understanding REST Assured Syntax
Almost every REST Assured test follows this structure:
given().when().then();
Let’s understand each method.
given()
This section contains everything required before sending the request.
Examples include:
- Base URI
- Headers
- Authentication
- Query parameters
- Path parameters
Example:
given().header("Content-Type", "application/json")
when()
This section specifies the HTTP method to execute.
Examples:
when().get()when().post()when().put()when().delete()
then()
This section validates the response returned by the server.
Examples:
- Status code
- Response body
- Headers
- Response time
Example:
then().statusCode(200);
Writing Your First REST Assured GET Test
Let’s use the free testing API:
https://reqres.in/api/users/2
Complete example:
import io.restassured.RestAssured;import org.testng.annotations.Test;public class FirstGetRequest { @Test public void getUserDetails() { RestAssured .given() .when() .get("https://reqres.in/api/users/2") .then() .statusCode(200); }}
This is the simplest REST Assured test you can write.
Understanding the Code
Step 1
given()
Currently, we are not sending:
- Headers
- Parameters
- Authentication
So it remains empty.
Step 2
when().get("https://reqres.in/api/users/2")
This sends a GET request to the API.
Step 3
then().statusCode(200);
This verifies that the server returned HTTP Status Code 200 OK.
If another status code is returned, the test fails.
Viewing the Response
Sometimes you want to see the API response.
Use:
.then().log().all();
Complete example:
RestAssured .given() .when() .get("https://reqres.in/api/users/2") .then() .log().all();
Sample output:
{ "data": { "id": 2, "email": "janet.weaver@reqres.in", "first_name": "Janet", "last_name": "Weaver" }}
Logging is especially useful during debugging.
Validating Multiple Conditions
Instead of checking only the status code, you can validate the response body as well.
import static io.restassured.RestAssured.*;import static org.hamcrest.Matchers.*;@Testpublic void validateUser() { given() .when() .get("https://reqres.in/api/users/2") .then() .statusCode(200) .body("data.first_name", equalTo("Janet")) .body("data.last_name", equalTo("Weaver"));}
Now REST Assured verifies:
- Status code
- First name
- Last name
If any validation fails, the test fails.
Using Static Imports
Most REST Assured projects use static imports.
Instead of writing:
RestAssured.given()
You can write:
given()
Import:
import static io.restassured.RestAssured.*;
This makes your tests cleaner and easier to read.
Printing the Response Body
To print the response manually:
String response =given().when().get("https://reqres.in/api/users/2").then().extract().asString();System.out.println(response);
Output:
{ ...}
This approach is useful when debugging or saving the response for later use.
Common Beginner Mistakes
Forgetting .then()
Incorrect:
given().when().get(url);
Correct:
given().when().get(url).then().statusCode(200);
Wrong URL
Even a small typo in the endpoint can result in:
- 404 Not Found
- 400 Bad Request
Always verify the endpoint before executing your test.
Missing Static Imports
If your IDE cannot recognize given(), make sure you’ve added:
import static io.restassured.RestAssured.*;
Forgetting Hamcrest Matchers
For response body validation, import:
import static org.hamcrest.Matchers.*;
Best Practices
- Keep one API validation per test where practical.
- Use static imports to improve readability.
- Validate both the status code and the response body.
- Log requests and responses during development to simplify debugging.
- Use reusable base URIs and request specifications as your test suite grows.
- Avoid hardcoding URLs throughout your project.
Key Takeaways
- A GET request retrieves data from an API without modifying it.
- Every REST Assured test follows the
given() → when() → then()pattern. given()defines the request,when()executes it, andthen()validates the response.- You can validate status codes, response bodies, headers, and more.
- Logging responses is invaluable when developing and troubleshooting API tests.
Frequently Asked Questions
What is a GET request in REST Assured?
A GET request retrieves data from a REST API. In REST Assured, you send it using the .get() method and then validate the response with assertions.
What is the purpose of given(), when(), and then()?
given()sets up the request (headers, parameters, authentication).when()sends the HTTP request.then()validates the API response.
Can I validate the response body in a GET request?
Yes. REST Assured supports validating JSON and XML responses using Hamcrest matchers such as equalTo(), containsString(), and hasItems().
Which public API can I use to practice REST Assured?
Popular options include:
- ReqRes
- JSONPlaceholder
- Restful Booker
- Fake Store API
These APIs are widely used for learning and experimenting with API automation.
What’s Next?
Now that you’ve learned how to send your first GET request, the next step is to build cleaner, reusable requests.
In the next article, we’ll explore REST Assured Request Specification, where you’ll learn how to define reusable configurations such as base URIs, headers, authentication, and common request settings to reduce code duplication and improve maintainability.
Related Articles
Discover more from Rotebit
Subscribe to get the latest posts sent to your email.

