Log in and receive a JWT access token and a refresh token
POST
https://api.testingapis.com/api/auth/jwt/loginUsers: admin / admin123 (role admin), user / user123 (role user). Access tokens expire after 15 minutes. Send them as Authorization: Bearer <accessToken>.
Request body
Optional. application/json, text/json, application/*+json, application/xml, text/xml, application/*+xml. Model: JwtAuthControllerLoginRequest. Username and password.
Example body
{"username": "admin", "password": "admin123"}Responses
- 200
- accessToken, tokenType, expiresIn, refreshToken and user.
- 400
- username or password missing.
- 401
- Wrong username or password.
Example
Request
curl -X POST "https://api.testingapis.com/api/auth/jwt/login" \
-H "Content-Type: application/json" \
-d '{"username": "admin", "password": "admin123"}'Response200 OK
{
"accessToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1laWRlbnRpZmllciI…
"tokenType": "Bearer",
"expiresIn": 900,
"refreshToken": "jrt_812bbc710f53465534cb39e4a0e98ba198616510",
"user": {"username": "admin", "role": "admin", "name": "Alice Admin}
}Code examples
JavaScript (fetch)
const response = await fetch("https://api.testingapis.com/api/auth/jwt/login", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: "{\"username\": \"admin\", \"password\": \"admin123\"}",
});
console.log(response.status, await response.text());Python (requests)
import requests
response = requests.request(
"POST",
"https://api.testingapis.com/api/auth/jwt/login",
headers={
"Content-Type": "application/json",
},
data="{\"username\": \"admin\", \"password\": \"admin123\"}".encode("utf-8"),
allow_redirects=False,
)
print(response.status_code, response.text)Java (HttpClient)
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Main {
public static void main(String[] args) throws Exception {
HttpClient client = HttpClient.newBuilder()
.followRedirects(HttpClient.Redirect.NEVER)
.build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.testingapis.com/api/auth/jwt/login"))
.header("Content-Type", "application/json")
.method("POST", HttpRequest.BodyPublishers.ofString("{\"username\": \"admin\", \"password\": \"admin123\"}"))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode() + " " + response.body());
}
}