Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
In molte situazioni, ti consigliamo di utilizzare i codici di stato HTTP comuni per indicare il buon esito o il fallimento della richiesta API di un utente. Ad esempio, se un utente sta tentando di recuperare un'entità inesistente, ti consigliamo di inviare un codice di stato HTTP 404 per indicare che non esiste alcuna entità con l'ID entity_id.
Puoi inviare questi codici di stato HTTP comuni sollevando un'eccezione fornita dalla libreria di endpoint come segue:
message='No entity with the id "%s" exists.'%entity_idraiseendpoints.NotFoundException(message)
Eccezione fornite da Endpoints Frameworks
La libreria di endpoint fornisce le seguenti eccezioni, corrispondenti a
codici di stato HTTP specifici:
Eccezione
Codice di stato HTTP corrispondente
endpoints.BadRequestException
HTTP 400
endpoints.UnauthorizedException
HTTP 401
endpoints.ForbiddenException
HTTP 403
endpoints.NotFoundException
HTTP 404
endpoints.InternalServerErrorException
HTTP 500
Codici di stato HTTP supportati
I framework Cloud Endpoints supportano un sottoinsieme di codici di stato HTTP nelle risposte dell'API. La seguente tabella descrive i codici supportati.
Codici di stato HTTP
Assistenza
HTTP 2xx
In genere, HTTP 200 viene assunto dai framework di endpoint se il metodo API restituisce un valore corretto. Se il tipo di risposta del metodo API è VoidMessage o il valore restituito del metodo API è None, viene impostato HTTP 204.
HTTP 3xx
I codici HTTP 3xx non sono supportati. L'utilizzo di codici HTTP 3xx comporta una risposta HTTP 404.
HTTP 4xx
Sono supportati solo i seguenti codici HTTP 4xx:
400
401
403
404
409
410
412
413
Qualsiasi altro codice HTTP 4xx viene restituito come errore 404, ad eccezione di quanto segue:
405 viene restituito come 501
408 viene restituito come 503
Errori HTTP 5xx
Tutti i codici di stato HTTP 5xx vengono convertiti in HTTP 503 nella risposta del client.
Creare classi di eccezione personalizzate
Se vuoi creare altre classi di eccezione per altri codici di stato HTTP, puoi farlo creando una sottoclasse di endpoints.ServiceException. Lo snippet riportato di seguito mostra come creare una classe di eccezione che rappresenti un codice di stato HTTP 409:
importendpointsimporthttplibclassConflictException(endpoints.ServiceException):"""Conflict exception that is mapped to a 409 response."""http_status=httplib.CONFLICT
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-07-10 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."]]],[]]