Passa al contenuto principale

curl

L'interfaccia GraphQL viene richiamata tramite richieste POST con un corpo JSON. Non è richiesta alcuna autenticazione.

Richiesta singola

curl -X POST https://data.bafu.admin.ch/api \
-H "Content-Type: application/json" \
--data '{"query":"{ water { observations { stations(where:{status:{_eq:\"Aufgebaut\"}}, limit: 5) { no name riverName } } } }"}'

La risposta segue la specifica GraphQL: una chiamata riuscita contiene un oggetto data, una chiamata fallita un oggetto errors.

Richiesta da file

Le richieste più lunghe vengono salvate in un file e inviate con --data-binary. Interruzioni di riga e virgolette sono mantenute.

cat > query.graphql <<'EOF'
{
water {
observations {
data_1day_mean(
where: {
station: { no: { _eq: "2009" } }
timestamp: { _gte: "2026-01-01T00:00:00Z", _lt: "2026-02-01T00:00:00Z" }
}
) {
timestamp
parameterName
value
unitSymbol
}
}
}
}
EOF

curl -X POST https://data.bafu.admin.ch/api \
-H "Content-Type: application/json" \
--data-binary "$(jq -Rs '{query: .}' < query.graphql)"

Richiesta con variabili

curl -X POST https://data.bafu.admin.ch/api \
-H "Content-Type: application/json" \
--data @- <<'EOF'
{
"query": "query DailyMean($from: AWSDateTime!, $to: AWSDateTime!, $station: String!) { water { observations { data_1day_mean(where: { station: { no: { _eq: $station } } timestamp: { _gte: $from, _lt: $to } }) { timestamp parameterName value unitSymbol } } } }",
"variables": {
"from": "2026-01-01T00:00:00Z",
"to": "2026-02-01T00:00:00Z",
"station": "2009"
}
}
EOF

Note

  • L'intestazione Content-Type: application/json è obbligatoria.
  • Le virgolette doppie all'interno della richiesta vanno protette (\") quando passate tramite --data '…'. La protezione non è necessaria se la richiesta viene letta da un file.
  • Lo stato HTTP è 200 anche in caso di richieste fallite. Il campo errors della risposta segnala i problemi.