2009-04-19 15 views

risposta

58

Per prima cosa è necessario dare un'occhiata allo HTTP 1.1 specification, in particolare alla sezione method definitions.

  • OPZIONI ottenere informazioni sul modo in cui il server consente di comunicare con.

    Richiesta:

    OPTIONS * HTTP/1.1 
    Host: example.com 
    

    Risposta:

    HTTP/1.1 200 OK 
    Date: … 
    Allow: OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE 
    Content-Length: 0 
    
  • GET recuperare una risorsa.

    Richiesta:

    GET /foo/bar HTTP/1.1 
    Host: example.com 
    

    Risposta:

    HTTP/1.1 200 OK 
    Date: … 
    Content-Type: text/html;charset=utf-8 
    Content-Length: 12345 
      
    <!DOCTYPE … 
    
  • TESTA Come GET, ma restituisce solo l'header HTTP.

    Richiesta:

    HEAD /foo/bar HTTP/1.1 
    Host: example.com 
    

    Risposta:

    HTTP/1.1 200 OK 
    Date: … 
    Content-Type: text/html;charset=utf-8 
    Content-Length: 12345 
    
  • POST Creare una nuova risorsa.

    Richiesta:

    POST /foo/bar HTTP/1.1 
    Host: example.com 
    Content-Type: application/x-www-form-urlencoded 
      
    action=addentry&subject=Hello,%20World 
    

    Risposta:

    HTTP/1.1 201 Created 
    Date: … 
    Content-Length: 0 
    Location: http://example.com/foo/bar   
    
  • PUT inviare i dati al server.

  • DELETE Eliminare una risorsa esistente.

  • TRACE Restituire le intestazioni di richiesta inviate dal client.

    Richiesta:

    TRACE /foo/bar HTTP/1.1 
    Host: example.com 
    

    Risposta:

    HTTP/1.1 200 OK 
    Date: … 
    Content-Length: 17 
      
    Host: example.com 
    

Non so esattamente se questi esempi sono corrette. Sentiti libero di correggerli.

+1

ottima risposta :) – dfa

+0

e come scrivere un metodo put –

+0

Anche come testare questi metodi? scrivili dove ottenere la risposta? –

13

È possibile sperimentare con i diversi metodi HTTP utilizzando lo strumento da riga di comando cURL. Per esempio:

curl --head http://www.google.co.uk 

HTTP/1.1 200 OK 
Cache-Control: private, max-age=0 
Date: Sun, 19 Apr 2009 15:33:24 GMT 
Expires: -1 
Content-Type: text/html; charset=ISO-8859-1 
Set-Cookie: PREF=ID=a2a414b9a84c8ffd:TM=1240155204:LM=1240155204:S=16kZnqzeSxIJT3jv; expires=Tue, 19-Apr-2011 15:33:24 GMT; path=/; domain=.google.co.uk 
Server: gws 
Transfer-Encoding: chunked 
  • L'opzione -X consente di specificare un metodo HTTP diverso da GET.
+2

Buon suggerimento, + 1. Nota che non tutti i browser supportano tutti i metodi. Ecco perché, ad esempio, Ruby on Rails utilizza solo GET e POST, e non PUT e DELETE. Se curl non supporta tutti i metodi, allora si può anche usare telnet per invocarli manualmente: vedi ad esempio http://tonycode.com/wiki/index.php?title=Making_HTTP_requests_via_telnet – Arjan

+0

D'accordo, l'ho già fatto con Telnet . –

Problemi correlati