Skip to content
CodeKruPro

Basic Authentication

30/62

Basic Authentication

HTTP Basic Auth sends credentials as a Base64-encoded header. REST Assured makes this easy:

given().auth().preemptive().basic("username", "password")

The preemptive() sends credentials with the first request (without waiting for a 401 challenge).

Credentials: username admin, password secret123

Task:

  1. GET /auth/basic with preemptive Basic auth
  2. Print the status code, then the message and user fields from the response body

Expected output:

Status: 200
Message: Basic auth successful
User: admin

About this lesson

REST Assured basic authentication: preemptive vs challenged credentials

Learn REST AssuredIntermediateAuthentication

How it works

HTTP Basic authentication puts a username and password in the Authorization header as Basic followed by the Base64 encoding of the two values joined by a colon. Base64 is an encoding, not encryption, so Basic auth is only acceptable over HTTPS.

REST Assured offers two modes. auth().preemptive().basic(user, pass) attaches the header to the first request. Plain auth().basic(user, pass) sends nothing at first and waits for the server to answer 401 with a WWW-Authenticate challenge, then repeats the call with credentials. An echo endpoint shows the difference clearly: the preemptive form arrives with the header, the challenged form arrives without it, because the echo never challenges.

testkru /auth/basic does send that challenge, so both modes reach a 200 here, but the challenged one costs an extra round trip. Wrong credentials get 401, a WWW-Authenticate header naming the realm, and an error message. On success the body has a message and the user the server decoded from your header.

About the assignment

Authenticate against the Basic auth endpoint with the course credentials, sending them up front, and print the status plus what the server says about you.

What you will do

  1. Start from given() and use auth(), then preemptive(), then basic() with the username and password from the task.
  2. GET /auth/basic and keep the Response.
  3. Print the status code after Status:.
  4. Read message and user with getString() and print them after Message: and User:.

Common mistakes

  • Dropping preemptive() against servers that do not challenge

    Many APIs answer a missing credential with 401 and no WWW-Authenticate header, or with 403. The challenged mode then never retries and the test fails though the password is right.

  • Building the header by hand

    It works, but it is easy to forget the colon or the Basic prefix. Let basic() do the encoding, and keep the raw value out of your code.

  • Logging credentials

    A log().all() on this request prints the Authorization header, and its Base64 value decodes straight back to the password. Blacklist the header in log config on real systems.

In a real test suite

Basic auth is common on internal tools and CI endpoints. Suites read the credentials from environment variables and put the auth scheme in a request specification, so no test file contains a password.

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.

Main.java
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import static io.restassured.RestAssured.*;

public class Main {
    public static void main(String[] args) {
        RestAssured.baseURI = "https://testkru.com/api";

        // TODO: GET /auth/basic with preemptive basic auth (admin / secret123)
        // TODO: Print status, message, and user from response
    }
}