Strumenti Utente

Strumenti Sito


programmazione:restapi:metodi_http

Metodi Http da usare nelle Rest Api

Autore: Fabio Di Matteo
Ultima revisione: 10/06/2025 13:41

Metodo HTTP Significato Prassi comune
GET Recupera risorse Utilizzato per ottenere dati dal server. Non modifica lo stato del server.
POST Crea una nuova risorsa Utilizzato per inviare dati al server per creare una nuova risorsa.
PUT Aggiorna una risorsa esistente Utilizzato per aggiornare completamente una risorsa esistente.
PATCH Aggiorna parzialmente una risorsa Utilizzato per apportare modifiche parziali a una risorsa esistente.
DELETE Elimina una risorsa Utilizzato per rimuovere una risorsa dal server.
HEAD Recupera solo le intestazioni Simile a GET, ma restituisce solo le intestazioni della risposta, senza il corpo.
OPTIONS Recupera le opzioni di comunicazione Utilizzato per descrivere le opzioni di comunicazione per la risorsa target.
CONNECT Stabilisce un tunnel Utilizzato per stabilire una connessione tunnel, tipicamente per HTTPS.
TRACE Diagnostica la richiesta Utilizzato per diagnosticare la richiesta, restituendo la richiesta stessa.

Codici di risposta HttP da utilizzare nelle REST Api

Codice di Risposta Descrizione Metodo HTTP
200 OK, richiesta eseguita con successo GET, POST, PUT, DELETE
201 Created, risorsa creata con successo POST
204 No Content, richiesta eseguita con successo, ma senza contenuto GET, POST, PUT, DELETE
400 Bad Request, richiesta non valida GET, POST, PUT, DELETE
401 Unauthorized, accesso non autorizzato GET, POST, PUT, DELETE
403 Forbidden, accesso proibito GET, POST, PUT, DELETE
404 Not Found, risorsa non trovata GET, POST, PUT, DELETE
405 Method Not Allowed, metodo non consentito GET, POST, PUT, DELETE
500 Internal Server Error, errore interno del server GET, POST, PUT, DELETE
501 Not Implemented, funzionalità non implementata GET, POST, PUT, DELETE
502 Bad Gateway, errore di gateway GET, POST, PUT, DELETE
503 Service Unavailable, servizio non disponibile GET, POST, PUT, DELETE

I codici di risposta possono essere raggruppati in categorie:

  • 1xx: informazioni
  • 2xx: successo
  • 3xx: ridirezionamento
  • 4xx: errore del client
  • 5xx: errore del server

Codici di risposta e relativa funzione PHP

Codice di Stato Funzione che genera e invia l'header
100 `header(“HTTP/1.1 100 Continue”);`
101 `header(“HTTP/1.1 101 Switching Protocols”);`
200 `header(“HTTP/1.1 200 OK”);`
201 `header(“HTTP/1.1 201 Created”);`
202 `header(“HTTP/1.1 202 Accepted”);`
203 `header(“HTTP/1.1 203 Non-Authoritative Information”);`
204 `header(“HTTP/1.1 204 No Content”);`
205 `header(“HTTP/1.1 205 Reset Content”);`
206 `header(“HTTP/1.1 206 Partial Content”);`
300 `header(“HTTP/1.1 300 Multiple Choices”);`
301 `header(“HTTP/1.1 301 Moved Permanently”);`
302 `header(“HTTP/1.1 302 Found”);`
303 `header(“HTTP/1.1 303 See Other”);`
304 `header(“HTTP/1.1 304 Not Modified”);`
305 `header(“HTTP/1.1 305 Use Proxy”);`
307 `header(“HTTP/1.1 307 Temporary Redirect”);`
308 `header(“HTTP/1.1 308 Permanent Redirect”);`
400 `header(“HTTP/1.1 400 Bad Request”);`
401 `header(“HTTP/1.1 401 Unauthorized”);`
402 `header(“HTTP/1.1 402 Payment Required”);`
403 `header(“HTTP/1.1 403 Forbidden”);`
404 `header(“HTTP/1.1 404 Not Found”);`
405 `header(“HTTP/1.1 405 Method Not Allowed”);`
406 `header(“HTTP/1.1 406 Not Acceptable”);`
407 `header(“HTTP/1.1 407 Proxy Authentication Required”);`
408 `header(“HTTP/1.1 408 Request Timeout”);`
409 `header(“HTTP/1.1 409 Conflict”);`
410 `header(“HTTP/1.1 410 Gone”);`
411 `header(“HTTP/1.1 411 Length Required”);`
412 `header(“HTTP/1.1 412 Precondition Failed”);`
413 `header(“HTTP/1.1 413 Payload Too Large”);`
414 `header(“HTTP/1.1 414 URI Too Long”);`
415 `header(“HTTP/1.1 415 Unsupported Media Type”);`
416 `header(“HTTP/1.1 416 Range Not Satisfiable”);`
417 `header(“HTTP/1.1 417 Expectation Failed”);`
426 `header(“HTTP/1.1 426 Upgrade Required”);`
500 `header(“HTTP/1.1 500 Internal Server Error”);`
501 `header(“HTTP/1.1 501 Not Implemented”);`
502 `header(“HTTP/1.1 502 Bad Gateway”);`
503 `header(“HTTP/1.1 503 Service Unavailable”);`
504 `header(“HTTP/1.1 504 Gateway Timeout”);`
505 `header(“HTTP/1.1 505 HTTP Version Not Supported”);`

esempio di utilizzo:

<?php
// Restituisce un codice di stato 200 (OK)
header("HTTP/1.1 200 OK");
echo "Richiesta riuscita!";
 
// Restituisce un codice di stato 404 (Not Found)
// header("HTTP/1.1 404 Not Found");
// echo "Errore 404: Pagina non trovata.";
 
// Restituisce un codice di stato 500 (Internal Server Error)
// header("HTTP/1.1 500 Internal Server Error");
// echo "Errore 500: Errore interno del server.";
 
// Restituisce un codice di stato 503 (Service Unavailable)
// header("HTTP/1.1 503 Service Unavailable");
// echo "Errore 503: Servizio non disponibile.";
 
// Restituisce un codice di stato 400 (Bad Request)
// header("HTTP/1.1 400 Bad Request");
// echo "Errore 400: Richiesta non valida.";
?>

Codici di risposta e relativa funzione Python con framework Bottle

Codice di Stato Esempio di Intestazione
100 `response.status = 100`
101 `response.status = 101`
200 `response.status = 200`
201 `response.status = 201`
202 `response.status = 202`
203 `response.status = 203`
204 `response.status = 204`
205 `response.status = 205`
206 `response.status = 206`
300 `response.status = 300`
301 `response.status = 301`
302 `response.status = 302`
303 `response.status = 303`
304 `response.status = 304`
305 `response.status = 305`
307 `response.status = 307`
308 `response.status = 308`
400 `response.status = 400`
401 `response.status = 401`
402 `response.status = 402`
403 `response.status = 403`
404 `response.status = 404`
405 `response.status = 405`
406 `response.status = 406`
407 `response.status = 407`
408 `response.status = 408`
409 `response.status = 409`
410 `response.status = 410`
411 `response.status = 411`
412 `response.status = 412`
413 `response.status = 413`
414 `response.status = 414`
415 `response.status = 415`
416 `response.status = 416`
417 `response.status = 417`
426 `response.status = 426`
500 `response.status = 500`
501 `response.status = 501`
502 `response.status = 502`
503 `response.status = 503`
504 `response.status = 504`
505 `response.status = 505`

ad esempio:

from bottle import Bottle, response, run
 
app = Bottle()
 
@app.route('/success')
def success():
    response.status = 200  # OK
    return "Richiesta riuscita!"
 
@app.route('/not_found')
def not_found():
    response.status = 404  # Not Found
    return "Errore 404: Pagina non trovata."
 
@app.route('/server_error')
def server_error():
    response.status = 500  # Internal Server Error
    return "Errore 500: Errore interno del server."
 
@app.route('/service_unavailable')
def service_unavailable():
    response.status = 503  # Service Unavailable
    return "Errore 503: Servizio non disponibile."
 
if __name__ == '__main__':
    run(app, host='localhost', port=8080)
programmazione/restapi/metodi_http.txt · Ultima modifica: 12/06/2025 14:38 da Fabio Di Matteo