Metodi di richiesta HTTP
Che cos'è HTTP?
L'Hypertext Transfer Protocol (HTTP) è progettato per abilitare le comunicazioni tra client e server.
HTTP funziona come un protocollo di richiesta-risposta tra un client e un server.
Esempio: un client (browser) invia una richiesta HTTP al server; quindi il server restituisce una risposta al client. La risposta contiene informazioni sullo stato della richiesta e può anche contenere il contenuto richiesto.
Metodi HTTP
- OTTENERE
- INVIARE
- METTERE
- TESTA
- ELIMINARE
- TOPPA
- OPZIONI
I due metodi HTTP più comuni sono: GET e POST.
Il metodo GET
GET viene utilizzato per richiedere dati da una risorsa specificata.
GET è uno dei metodi HTTP più comuni.
Si noti che la stringa di query (coppie nome/valore) viene inviata nell'URL di una richiesta GET:
/test/demo_form.php?name1=value1&name2=value2
Alcune altre note sulle richieste GET:
- Le richieste GET possono essere memorizzate nella cache
- Le richieste GET rimangono nella cronologia del browser
- Le richieste GET possono essere aggiunte ai preferiti
- Le richieste GET non dovrebbero mai essere utilizzate quando si tratta di dati sensibili
- Le richieste GET hanno limitazioni di lunghezza
- Le richieste GET vengono utilizzate solo per richiedere dati (non modificare)
Il metodo POST
POST viene utilizzato per inviare dati a un server per creare/aggiornare una risorsa.
I dati inviati al server con POST sono memorizzati nel corpo della richiesta HTTP:
POST /test/demo_form.php HTTP/1.1
Host: w3schools.com
name1=value1&name2=value2
POST è uno dei metodi HTTP più comuni.
Alcune altre note sulle richieste POST:
- Le richieste POST non vengono mai memorizzate nella cache
- Le richieste POST non rimangono nella cronologia del browser
- Le richieste POST non possono essere salvate nei segnalibri
- Le richieste POST non hanno restrizioni sulla lunghezza dei dati
Il metodo PUT
PUT viene utilizzato per inviare dati a un server per creare/aggiornare una risorsa.
La differenza tra POST e PUT è che le richieste PUT sono idempotenti. Cioè, chiamare più volte la stessa richiesta PUT produrrà sempre lo stesso risultato. Al contrario, chiamare ripetutamente una richiesta POST ha gli effetti collaterali della creazione della stessa risorsa più volte.
Il metodo HEAD
HEAD è quasi identico a GET, ma senza il corpo di risposta.
In altre parole, se GET /users restituisce un elenco di utenti, HEAD /users farà la stessa richiesta ma non restituirà l'elenco di utenti.
Le richieste HEAD sono utili per verificare cosa restituirà una richiesta GET prima di effettuare effettivamente una richiesta GET, ad esempio prima di scaricare un file di grandi dimensioni o il corpo della risposta.
Il metodo DELETE
Il metodo DELETE elimina la risorsa specificata.
Il metodo OPZIONI
Il metodo OPTIONS descrive le opzioni di comunicazione per la risorsa di destinazione.
Confronta GET e POST
La tabella seguente confronta i due metodi HTTP: GET e POST.
GET | POST | |
---|---|---|
BACK button/Reload | Harmless | Data will be re-submitted (the browser should alert the user that the data are about to be re-submitted) |
Bookmarked | Can be bookmarked | Cannot be bookmarked |
Cached | Can be cached | Not cached |
Encoding type | application/x-www-form-urlencoded | application/x-www-form-urlencoded or multipart/form-data. Use multipart encoding for binary data |
History | Parameters remain in browser history | Parameters are not saved in browser history |
Restrictions on data length | Yes, when sending data, the GET method adds the data to the URL; and the length of a URL is limited (maximum URL length is 2048 characters) | No restrictions |
Restrictions on data type | Only ASCII characters allowed | No restrictions. Binary data is also allowed |
Security | GET is less secure compared to POST because data sent is part of the URL Never use GET when sending passwords or other sensitive information! |
POST is a little safer than GET because the parameters are not stored in browser history or in web server logs |
Visibility | Data is visible to everyone in the URL | Data is not displayed in the URL |