*405* `Method Not Allowed` Raise if the server used a method the resource does not handle. For example `POST` if the resource is view only. Especially useful for REST. The first argument for this exception should be a list of allowed methods. Strictly speaking the response would
| 362 | |
| 363 | |
| 364 | class MethodNotAllowed(HTTPException): |
| 365 | """*405* `Method Not Allowed` |
| 366 | |
| 367 | Raise if the server used a method the resource does not handle. For |
| 368 | example `POST` if the resource is view only. Especially useful for REST. |
| 369 | |
| 370 | The first argument for this exception should be a list of allowed methods. |
| 371 | Strictly speaking the response would be invalid if you don't provide valid |
| 372 | methods in the header which you can do with that list. |
| 373 | """ |
| 374 | |
| 375 | code = 405 |
| 376 | description = "The method is not allowed for the requested URL." |
| 377 | |
| 378 | def __init__( |
| 379 | self, |
| 380 | valid_methods: t.Iterable[str] | None = None, |
| 381 | description: str | None = None, |
| 382 | response: SansIOResponse | None = None, |
| 383 | ) -> None: |
| 384 | """Takes an optional list of valid http methods |
| 385 | starting with werkzeug 0.3 the list will be mandatory.""" |
| 386 | super().__init__(description=description, response=response) |
| 387 | self.valid_methods = valid_methods |
| 388 | |
| 389 | def get_headers( |
| 390 | self, |
| 391 | environ: WSGIEnvironment | None = None, |
| 392 | scope: dict[str, t.Any] | None = None, |
| 393 | ) -> list[tuple[str, str]]: |
| 394 | headers = super().get_headers(environ, scope) |
| 395 | if self.valid_methods: |
| 396 | headers.append(("Allow", ", ".join(self.valid_methods))) |
| 397 | return headers |
| 398 | |
| 399 | |
| 400 | class NotAcceptable(HTTPException): |