Esempi
Fissa i blocchi di codice in una barra laterale destra su desktop, così i lettori possono consultarli mentre scorrono. Supporta linguaggi a schede.
Blocchi di codice fissati in una barra laterale destra su desktop, così i lettori possono consultarli mentre scorrono. Su dispositivi mobili vengono visualizzati in linea.
RequestExample
Usa RequestExample per mostrare il codice delle richieste API. Più blocchi di codice creano viste a schede.
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 creare un'interfaccia a 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 diversi scenari di risposta con schede separate:
{
"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
Per la documentazione delle API, usa entrambi i componenti. 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 supporta due formati per i codici di stato:
```json 200: Success // Colon separator
```json 400 - Bad Request // Dash separator
Entrambi i formati vengono analizzati nello stesso modo e visualizzano il codice di stato con la relativa descrizione nella scheda.
Qual è il prossimo passo?
