Passa al contenuto principale

Appendice A. Rappresentazione delle risorse e dati di rappresentazione

The following examples show how representation metadata, content transformations, and methods impact the message and content. These examples a not exhaustive.

I seguenti esempi mostrano come i metadati di rappresentazione, le trasformazioni del contenuto e i metodi influiscono sul messaggio e sul contenuto. Questi esempi non sono esaustivi.

Unless otherwise indicated, the examples are based on the JSON object {"hello": "world"} followed by an LF. When the content contains non-printable characters (e.g., when it is encoded), it is shown as a sequence of hex-encoded bytes.

Se non diversamente indicato, gli esempi si basano sull'oggetto JSON {"hello": "world"} seguito da un LF. Quando il contenuto contiene caratteri non stampabili (ad esempio, quando è codificato), viene mostrato come una sequenza di byte codificati in esadecimale.

Consider a client that wishes to upload a JSON object using the PUT method. It could do this using the application/json Content-Type without any content coding.

Consideriamo un client che desidera caricare un oggetto JSON utilizzando il metodo PUT. Potrebbe farlo utilizzando il Content-Type application/json senza alcuna codifica del contenuto.

PUT /entries/1234 HTTP/1.1
Host: foo.example
Content-Type: application/json
Content-Length: 19

{"hello": "world"}

Figure 1: Request Containing a JSON Object without Any Content Coding

Figura 1: Richiesta contenente un oggetto JSON senza alcuna codifica del contenuto

However, the use of content coding is quite common. The client could also upload the same data with a GZIP coding (Section 8.4.1.3 of [HTTP]). Note that in this case, the Content-Length contains a larger value due to the coding overheads.

Tuttavia, l'uso della codifica del contenuto è piuttosto comune. Il client potrebbe anche caricare gli stessi dati con una codifica GZIP (Sezione 8.4.1.3 di [HTTP]). Si noti che in questo caso, il Content-Length contiene un valore maggiore a causa del sovraccarico della codifica.

PUT /entries/1234 HTTP/1.1
Host: foo.example
Content-Type: application/json
Content-Encoding: gzip
Content-Length: 39

1F 8B 08 00 88 41 37 64 00 FF
AB 56 CA 48 CD C9 C9 57 B2 52
50 2A CF 2F CA 49 51 AA E5 02
00 D9 E4 31 E7 13 00 00 00

Figure 2: Request Containing a GZIP-Encoded JSON Object

Figura 2: Richiesta contenente un oggetto JSON codificato con GZIP

Sending the GZIP-coded data without indicating it via Content-Encoding means that the content is malformed. In this case, the server can reply with an error.

L'invio dei dati codificati con GZIP senza indicarlo tramite Content-Encoding significa che il contenuto è malformato. In questo caso, il server può rispondere con un errore.

PUT /entries/1234 HTTP/1.1
Host: foo.example
Content-Type: application/json

Content-Length: 39

1F 8B 08 00 88 41 37 64 00 FF
AB 56 CA 48 CD C9 C9 57 B2 52
50 2A CF 2F CA 49 51 AA E5 02
00 D9 E4 31 E7 13 00 00 00

Figure 3: Request Containing Malformed JSON

Figura 3: Richiesta contenente JSON malformato

HTTP/1.1 400 Bad Request

Figure 4: An Error Response for Malformed Content

Figura 4: Una risposta di errore per contenuto malformato

A Range-Request affects the transferred message content. In this example, the client is accessing the resource at /entries/1234, which is the JSON object {"hello": "world"} followed by an LF. However, the client has indicated a preferred content coding and a specific byte range.

Una richiesta di intervallo (Range-Request) influisce sul contenuto del messaggio trasferito. In questo esempio, il client sta accedendo alla risorsa su /entries/1234, che è l'oggetto JSON {"hello": "world"} seguito da un LF. Tuttavia, il client ha indicato una codifica del contenuto preferita e un intervallo di byte specifico.

GET /entries/1234 HTTP/1.1
Host: foo.example
Accept-Encoding: gzip
Range: bytes=1-7

Figure 5: Request for Partial Content

Figura 5: Richiesta di contenuto parziale

The server satisfies the client request by responding with a partial representation (equivalent to the first 10 bytes of the JSON object displayed in whole in Figure 2).

Il server soddisfa la richiesta del client rispondendo con una rappresentazione parziale (equivalente ai primi 10 byte dell'oggetto JSON visualizzato per intero nella Figura 2).

HTTP/1.1 206 Partial Content
Content-Encoding: gzip
Content-Type: application/json
Content-Range: bytes 0-9/39

1F 8B 08 00 A5 B4 BD 62 02 FF

Figure 6: Partial Response from a GZIP-Encoded Representation

Figura 6: Risposta parziale da una rappresentazione codificata con GZIP

Aside from content coding or range requests, the method can also affect the transferred message content. For example, the response to a HEAD request does not carry content, but this example case includes Content-Length; see Section 8.6 of [HTTP].

Oltre alla codifica del contenuto o alle richieste di intervallo, anche il metodo può influire sul contenuto del messaggio trasferito. Ad esempio, la risposta a una richiesta HEAD non trasporta contenuto, ma questo caso di esempio include Content-Length; vedere la Sezione 8.6 di [HTTP].

HEAD /entries/1234 HTTP/1.1
Host: foo.example
Accept: application/json
Accept-Encoding: gzip

Figure 7: HEAD Request

Figura 7: Richiesta HEAD

HTTP/1.1 200 OK
Content-Type: application/json
Content-Encoding: gzip
Content-Length: 39

Figure 8: Response to HEAD Request (Empty Content)

Figura 8: Risposta alla richiesta HEAD (contenuto vuoto)

Finally, the semantics of a response might decouple the target URI from the enclosed representation. In the example below, the client issues a POST request directed to /authors/, but the response includes a Content-Location header field indicating that the enclosed representation refers to the resource available at /authors/123. Note that Content-Length is not sent in this example.

Infine, la semantica di una risposta potrebbe disaccoppiare l'URI di destinazione dalla rappresentazione inclusa. Nell'esempio seguente, il client emette una richiesta POST diretta a /authors/, ma la risposta include un campo di intestazione Content-Location che indica che la rappresentazione inclusa si riferisce alla risorsa disponibile su /authors/123. Si noti che Content-Length non viene inviato in questo esempio.

POST /authors/ HTTP/1.1
Host: foo.example
Accept: application/json
Content-Type: application/json

{"author": "Camilleri"}

Figure 9: POST Request

Figura 9: Richiesta POST

HTTP/1.1 201 Created
Content-Type: application/json
Content-Location: /authors/123
Location: /authors/123

{"id": "123", "author": "Camilleri"}

Figure 10: Response with Content-Location Header

Figura 10: Risposta con intestazione Content-Location