Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
In vielen Situationen möchten Sie vielleicht gängige HTTP-Statuscodes verwenden, um einem Nutzer anzuzeigen, ob seine API-Anfrage erfolgreich war oder nicht. Wenn ein Nutzer beispielsweise versucht, eine nicht vorhandene Entität abzurufen, können Sie einen HTTP-404-Statuscode senden, der angibt, dass keine Entität mit der ID entity_id vorhanden ist.
Sie können gängige HTTP-Statuscodes senden. Dazu geben Sie eine von der Endpoints-Bibliothek bereitgestellte Ausnahme aus:
message='No entity with the id "%s" exists.'%entity_idraiseendpoints.NotFoundException(message)
Von Endpoints Frameworks bereitgestellte Ausnahmen
Die Endpoints-Bibliothek gibt die folgenden Ausnahmen aus. Diese entsprechen speziellen HTTP-Statuscodes:
Ausnahme
Zugehöriger HTTP-Statuscode
endpoints.BadRequestException
HTTP 400
endpoints.UnauthorizedException
HTTP 401
endpoints.ForbiddenException
HTTP 403
endpoints.NotFoundException
HTTP 404
endpoints.InternalServerErrorException
HTTP 500
Unterstützte HTTP-Statuscodes
Cloud Endpoints Frameworks unterstützt in API-Antworten einen Teil der HTTP-Statuscodes. Die unterstützten Codes werden in der folgenden Tabelle beschrieben.
HTTP-Statuscodes
Unterstützung
HTTP 2xx
Endpoints Frameworks geht bei einer erfolgreichen Rückgabe der API-Methode normalerweise von HTTP 200 aus. Wenn der Antworttyp der API-Methode VoidMessage oder der Rückgabewert der API-Methode None ist, wird stattdessen HTTP 204 festgelegt.
HTTP 3xx
HTTP 3xx-Codes werden nicht unterstützt. Bei der Verwendung von HTTP 3xx-Codes wird HTTP 404 zurückgegeben.
HTTP 4xx
Es werden nur die folgenden HTTP 4xx-Codes unterstützt:
400
401
403
404
409
410
412
413
Für alle anderen HTTP 4xx-Codes wird der Fehler 404 zurückgegeben. Dies gilt jedoch nicht für die folgenden Codes:
Für 405 wird 501 zurückgegeben.
Für 408 wird 503 zurückgegeben.
HTTP 5xx
Alle HTTP 5xx-Statuscodes werden in der Client-Antwort in HTTP 503 konvertiert.
Eigene Ausnahmeklassen erstellen
Wenn Sie andere Ausnahmeklassen für andere HTTP-Statuscodes erstellen möchten, können Sie diese aus endpoints.ServiceException ableiten. Das folgende Snippet zeigt, wie Sie eine Ausnahmeklasse erstellen, die den HTTP-Statuscode 409 repräsentiert:
importendpointsimporthttplibclassConflictException(endpoints.ServiceException):"""Conflict exception that is mapped to a 409 response."""http_status=httplib.CONFLICT
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2025-06-16 (UTC)."],[[["Endpoints Frameworks allows for the use of common HTTP status codes to indicate the success or failure of API requests, such as using a `404` error to indicate an entity does not exist."],["Specific exceptions, like `BadRequestException`, `UnauthorizedException`, `ForbiddenException`, `NotFoundException`, and `InternalServerErrorException`, are provided by the endpoints library and correspond to specific HTTP error codes."],["Only a subset of HTTP status codes are supported, where HTTP `200` or `204` is used for success, 3xx codes result in a `404` error, specific 4xx codes are supported, and all 5xx codes are converted to `503`."],["Uncaught exceptions in the application will result in an HTTP `503` error from the backend API."],["Custom exception classes for other HTTP status codes can be made by subclassing `endpoints.ServiceException`, as shown in the example of creating a `ConflictException` for an HTTP 409 status code."]]],[]]