Allow only tokens with the admin role
GET
https://api.testingapis.com/api/auth/roles/adminAllow only tokens with the admin role (401 vs 403 demo).
Header: Authorization: Bearer <token> with one of the test tokens: admin-token (roles admin, user; scopes read, write, delete), user-token (role user; scopes read, write), readonly-token (no roles; scope read). Only admin-token passes.
Bearer token — admin-token
Responses
- 200
- Token has the admin role.
- 401
- Token missing or not one of the test tokens.
- 403
- Valid token without the admin role.
Example
Request
curl -X GET "https://api.testingapis.com/api/auth/roles/admin" \
-H "Authorization: Bearer admin-token"Response200 OK
{
"authorized": true,
"user": "Alice Admin",
"roles": ["admin", "user"],
"scopes": ["read", "write", "delete"]
}Code examples
JavaScript (fetch)
const response = await fetch("https://api.testingapis.com/api/auth/roles/admin", {
headers: {
"Authorization": "Bearer admin-token",
},
});
console.log(response.status, await response.text());Python (requests)
import requests
response = requests.request(
"GET",
"https://api.testingapis.com/api/auth/roles/admin",
headers={
"Authorization": "Bearer admin-token",
},
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/roles/admin"))
.header("Authorization", "Bearer admin-token")
.method("GET", HttpRequest.BodyPublishers.noBody())
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode() + " " + response.body());
}
}