Esempi
Blocca i blocchi di codice, con schede opzionali per linguaggio, in una barra laterale destra fissa che resta visibile durante lo scorrimento su desktop.
Questi componenti fissano i blocchi di codice in una barra laterale destra sticky su desktop. Il codice resta visibile mentre il lettore scorre la pagina. Su mobile, i blocchi vengono mostrati in linea.
RequestExample
Usa RequestExample per mostrare il codice delle richieste API. Ogni blocco di codice aggiuntivo aggiunge una scheda.
curl -X POST https://api.example.com/users \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{"name": "John Doe", "email": "john@example.com"}'Utilizzo:
<RequestExample>
```bash cURL
curl -X POST https://api.example.com/users \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{"name": "John Doe", "email": "john@example.com"}'
```
</RequestExample>
ResponseExample
Usa ResponseExample per mostrare le risposte API. Includi un codice di stato e una descrizione dopo l'identificatore del linguaggio.
{
"id": "usr_123",
"name": "John Doe",
"email": "john@example.com",
"created_at": "2024-01-15T10:30:00Z"
}Utilizzo:
<ResponseExample>
```json 200: Success
{
"id": "usr_123",
"name": "John Doe",
"email": "john@example.com",
"created_at": "2024-01-15T10:30:00Z"
}
```
</ResponseExample>
Il codice di stato determina il colore dell'indicatore:
- Verde: codici di successo 2xx
- Blu: codici di reindirizzamento 3xx
- Ambra: errori client 4xx
- Rosso: errori server 5xx
Più linguaggi
Aggiungi più blocchi di codice per ottenere le schede:
curl -X GET https://api.example.com/users/123 \
-H "Authorization: Bearer $TOKEN"Utilizzo:
<RequestExample>
```bash cURL
curl -X GET https://api.example.com/users/123 \
-H "Authorization: Bearer $TOKEN"
```
```python Python
import requests
response = requests.get(
"https://api.example.com/users/123",
headers={"Authorization": f"Bearer {TOKEN}"}
)
user = response.json()
```
```javascript JavaScript
const response = await fetch("https://api.example.com/users/123", {
headers: {
"Authorization": `Bearer ${TOKEN}`
}
});
const user = await response.json();
```
```go Go
package main
import (
"fmt"
"io"
"net/http"
)
func main() {
req, _ := http.NewRequest("GET", "https://api.example.com/users/123", nil)
req.Header.Set("Authorization", "Bearer TOKEN")
resp, _ := (&http.Client{}).Do(req)
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
fmt.Println(string(body))
}
```
```ruby Ruby
require 'net/http'
require 'uri'
require 'json'
uri = URI("https://api.example.com/users/123")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new(uri)
request["Authorization"] = "Bearer TOKEN"
response = http.request(request)
puts JSON.parse(response.body)
```
</RequestExample>
Più codici di risposta
Mostra ogni codice di risposta nella propria scheda:
{
"id": "usr_123",
"name": "John Doe",
"email": "john@example.com"
}Utilizzo:
<ResponseExample>
```json 200: Success
{
"id": "usr_123",
"name": "John Doe",
"email": "john@example.com"
}
```
```json 400: Bad Request
{
"error": "validation_error",
"message": "Email is required"
}
```
```json 404: Not Found
{
"error": "not_found",
"message": "User not found"
}
```
</ResponseExample>
Utilizzo combinato
Usa RequestExample e ResponseExample insieme in una pagina API. La richiesta appare sopra la risposta nella barra laterale:
curl -X POST https://api.example.com/orders \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"product_id": "prod_456",
"quantity": 2
}'{
"id": "ord_789",
"product_id": "prod_456",
"quantity": 2,
"status": "pending",
"created_at": "2024-01-15T10:30:00Z"
}Utilizzo:
<RequestExample>
```bash cURL
curl -X POST https://api.example.com/orders \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"product_id": "prod_456",
"quantity": 2
}'
```
</RequestExample>
<ResponseExample>
```json 201: Created
{
"id": "ord_789",
"product_id": "prod_456",
"quantity": 2,
"status": "pending",
"created_at": "2024-01-15T10:30:00Z"
}
```
</ResponseExample>
Formati dei codici di stato
ResponseExample accetta due formati per il codice di stato:
```json 200: Success // Colon separator
```json 400 - Bad Request // Dash separator
Jamdesk li interpreta allo stesso modo. La scheda mostra il codice di stato e la sua descrizione.
