HTTP

Giả lập phản hồi 405 Method Not Allowed

The HTTP method is not allowed for the requested resource. The response includes an Allow header listing valid methods.

Xem tài liệu tham khảo đầy đủ →

1 Phản hồi HTTP

Phản hồi HTTP
HTTP/1.1 405 Method Not Allowed
Content-Type: application/json
Date: Tue, 25 Feb 2026 12:00:00 GMT
Allow: GET, HEAD
Content-Length: 64

{
  "error": "method_not_allowed",
  "message": "Method not allowed"
}

2 Kiểm tra với curl

terminal
curl -i https://httpbin.org/status/405

3 Trả về 405 Method Not Allowed trong Framework của bạn

django
from django.http import JsonResponse


def my_view(request):
    return JsonResponse(
        {"error": "method_not_allowed", "message": "Method not allowed"},
        status=405,
    )
flask
from flask import Flask, jsonify

app = Flask(__name__)


@app.route("/endpoint")
def my_endpoint():
    return jsonify({"error": "method_not_allowed", "message": "Method not allowed"}), 405
fastapi
from fastapi import FastAPI
from fastapi.responses import JSONResponse

app = FastAPI()


@app.get("/endpoint")
def my_endpoint():
    return JSONResponse(
        content={"error": "method_not_allowed", "message": "Method not allowed"},
        status_code=405,
    )
express.js
// Express.js
app.get('/endpoint', (req, res) => {
  res.status(405).json({"error": "method_not_allowed", "message": "Method not allowed"});
});
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(405)
            .body(Map.of("error", "method_not_allowed",
                         "message", "Method Not Allowed"));
    }
}
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(405)
    json.NewEncoder(w).Encode(map[string]string{
        "error":   "method_not_allowed",
        "message": "Method Not Allowed",
    })
}
ruby on rails
class MyController < ApplicationController
  def my_action
    render json: {"error": "method_not_allowed", "message": "Method not allowed"},
           status: :method_not_allowed
  end
end
asp.net core
// ASP.NET Core Minimal API
app.MapGet("/endpoint", () =>
    Results.Json(
        new { error = "method_not_allowed", message = "Method Not Allowed" },
        statusCode: 405
    )
);

Thêm trang giả lập HTTP