About this lesson
REST Assured GET request: send your first call and read the status code
Learn REST AssuredBeginnerGetting Started
How it works
REST Assured is a Java library for driving HTTP APIs from test code. There is no browser and no page to render: the library builds an HTTP request, sends it through Apache HttpClient, waits for the reply and wraps it in an io.restassured.response.Response. That object carries the three things every API check starts from: the status line, the response headers and the body.
The static field RestAssured.baseURI holds the scheme and host shared by every request, so a call only names the path that follows it. When it is never set, requests go to http://localhost on port 8080 (the constants DEFAULT_URI and DEFAULT_PORT), which is why a program that never sets it fails with java.net.ConnectException: Connection refused instead of reaching the practice API.
The status code is the server's one-number summary of what happened. getStatusCode() and its shorter twin statusCode() both return it as an int, while getStatusLine() gives the whole line, protocol and reason phrase included, such as HTTP/1.1 200 OK.
About the assignment
The starter already sets RestAssured.baseURI to the practice API, so your code only deals with paths. You send a single GET to the user collection and report which status code came back, the first fact any API test confirms.
What you will do
- Call the static
RestAssured.get()method with the/userspath. - Keep what it returns in a variable of type
Response, the import is already in the starter. - Ask that response for its status code with
getStatusCode()orstatusCode(). - Print the number after the label
Status Code:with one space between them.
Common mistakes
Leaving the scheme off baseURI
Writing the host without
https://does not quietly fall back to a default. As soon as the request is built, REST Assured throwsIllegalStateException: Target host is null, because it cannot tell a host from a path without the scheme.Treating a 4xx or 5xx reply as an exception
REST Assured hands back a 404 or 500 response like any other, no exception involved. If your code prints or uses the body without looking at the status, a broken endpoint looks like a success.
Forgetting that baseURI is global
RestAssured.baseURIis a static field, so every request in the same JVM shares it. One JUnit test that points it at another server silently changes the target of every test that runs after it.
In a real test suite
In a JUnit 5 class the base URI is normally set once in a @BeforeAll method, or moved into a RequestSpecification built with RequestSpecBuilder, so each @Test method contains only its request and its checks. A status check on a list endpoint like this one is the usual smoke test that the API is reachable before the deeper checks run.
Related lessons
- Reading the Response BodyRequest a single user and read fields from its JSON body with
jsonPath(). - Validate 200 OKTurn the printed status code into a real assertion with
statusCode(). - Base URI SpecificationMove the base URI out of the static field and into a reusable request specification.
To try one of these calls on any API endpoint without a task, open the Online Selenium Java Compiler (REST Assured is included).
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
public class Main {
public static void main(String[] args) {
RestAssured.baseURI = "https://testkru.com/api";
// TODO: Make a GET request to "/users" and store in a Response variable
// TODO: Print the status code in format: "Status Code: <code>"
}
}