Return protected data behind Basic auth
GET
https://api.testingapis.com/api/AuthTest/secure-dataCredentials: Admin / Admin@1234, sent as Authorization: Basic QWRtaW46QWRtaW5AMTIzNA==.
HTTP Basic — Admin / Admin@1234
Responses
- 200
- Protected data.
- 401
- Header missing or wrong credentials (empty body).
Example
Request
curl -X GET "https://api.testingapis.com/api/AuthTest/secure-data" \
-H "Authorization: Basic QWRtaW46QWRtaW5AMTIzNA=="Response200 OK
{"message": "Authentication successful", "data": "This is protected data"}Code examples
JavaScript (fetch)
const response = await fetch("https://api.testingapis.com/api/AuthTest/secure-data", {
headers: {
"Authorization": "Basic QWRtaW46QWRtaW5AMTIzNA==",
},
});
console.log(response.status, await response.text());Python (requests)
import requests
response = requests.request(
"GET",
"https://api.testingapis.com/api/AuthTest/secure-data",
headers={
"Authorization": "Basic QWRtaW46QWRtaW5AMTIzNA==",
},
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/AuthTest/secure-data"))
.header("Authorization", "Basic QWRtaW46QWRtaW5AMTIzNA==")
.method("GET", HttpRequest.BodyPublishers.noBody())
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode() + " " + response.body());
}
}