HTTP
محاكاة استجابة 401 Unauthorized
The request requires user authentication. The response includes a WWW-Authenticate header indicating the authentication scheme.
عرض المرجع الكامل →1 استجابة HTTP
HTTP/1.1 401 Unauthorized
Content-Type: application/json
Date: Tue, 25 Feb 2026 12:00:00 GMT
Content-Length: 63
{
"error": "unauthorized",
"message": "Authentication required"
}
2 اختبار باستخدام curl
curl -i https://httpbin.org/status/401
3 إرجاع 401 Unauthorized في إطار العمل الخاص بك
django
from django.http import JsonResponse
def my_view(request):
return JsonResponse(
{"error": "unauthorized", "message": "Authentication required"},
status=401,
)
flask
from flask import Flask, jsonify
app = Flask(__name__)
@app.route("/endpoint")
def my_endpoint():
return jsonify({"error": "unauthorized", "message": "Authentication required"}), 401
fastapi
from fastapi import FastAPI
from fastapi.responses import JSONResponse
app = FastAPI()
@app.get("/endpoint")
def my_endpoint():
return JSONResponse(
content={"error": "unauthorized", "message": "Authentication required"},
status_code=401,
)
express.js
// Express.js
app.get('/endpoint', (req, res) => {
res.status(401).json({"error": "unauthorized", "message": "Authentication required"});
});
spring boot
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@RestController
public class MyController {
@GetMapping("/endpoint")
public ResponseEntity<Map<String, Object>> myEndpoint() {
return ResponseEntity
.status(401)
.body(Map.of("error", "unauthorized",
"message", "Unauthorized"));
}
}
go net/http
package main
import (
"encoding/json"
"net/http"
)
func myHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(401)
json.NewEncoder(w).Encode(map[string]string{
"error": "unauthorized",
"message": "Unauthorized",
})
}
ruby on rails
class MyController < ApplicationController
def my_action
render json: {"error": "unauthorized", "message": "Authentication required"},
status: :unauthorized
end
end
asp.net core
// ASP.NET Core Minimal API
app.MapGet("/endpoint", () =>
Results.Json(
new { error = "unauthorized", message = "Unauthorized" },
statusCode: 401
)
);