influxdata/influxdb · error · Error::SerdeJson

error decoding query body

Error message

error decoding query body: {0}

What it means

`Error::SerdeJson` wraps `serde_json::Error` and is thrown when a JSON request body cannot be decoded into the v1 API's expected query parameters struct. The `#[from]` conversion makes any JSON deserialization failure in the request body produce this error.

Solutions

  1. Validate the JSON body parses and matches the expected schema (fields `db`, `q` as strings).
  2. Ensure the Content-Type is JSON only when the body actually is JSON; otherwise use form encoding.
  3. Deserialize locally with `serde_json::from_str::<Params>` to reproduce and pinpoint the failing field.

Example fix

// before
{"db": 123, "q": "SELECT 1"}  // wrong type
// after
{"db": "mydb", "q": "SELECT 1"}
Defensive patterns

Strategy: validation

Validate before calling

fn json_body_ok(body: &str) -> Result<(), serde_json::Error> {
    let v: serde_json::Value = serde_json::from_str(body)?;
    if v.get("db").and_then(|d| d.as_str()).is_none() { return Err(serde_json::Error::custom("'db' must be a string")); }
    Ok(())
}

Type guard

fn is_serde_json_error(e: &iox_v1_query_api::Error) -> bool {
    matches!(e, iox_v1_query_api::Error::SerdeJson(_))
}

Try / catch

let body = serde_json::json!({"db": db, "q": q}).to_string();
json_body_ok(&body)?;
let resp = client.post("/query").header("content-type", "application/json").body(body).send().await?;

Prevention

When it happens

Trigger: POSTing `application/json` bodies to the query endpoint with malformed JSON, wrong field types (e.g. number where string expected), or unknown/missing required fields in the deserialization target.

Common situations: Hand-written JSON payloads with syntax mistakes, clients sending JSON when the endpoint expects urlencoded form data, or schema drift after an API version bump.

Understand the failure class

Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.

Related errors


AI-assisted analysis of influxdata/influxdb@06200ef96b (2026-09-19). Data as JSON: /api/errors/bb6cb6ee81811277. Report an issue: GitHub.

Appendix: source

Thrown at core/iox_v1_query_api/src/error.rs:46

    /// Missing parameters for query
    #[error("missing query parameters 'db' and 'q'")]
    MissingQueryParams,

    #[error("error decoding multipart file upload: {0}")]
    MultipartFile(String),

    #[error("Invalid UTF8: {message} {error}")]
    Utf8 {
        message: &'static str,
        error: String,
    },

    /// Serde decode error
    #[error("error decoding params from url: {0}")]
    SerdeUrlDecoding(#[from] serde_urlencoded::de::Error),

    // SerdeJsonError
    #[error("error decoding query body: {0}")]
    SerdeJson(#[from] serde_json::Error),

    #[error("datafusion error: {0}")]
    Datafusion(#[from] DataFusionError),

    #[error("error in InfluxQL statement: {0}")]
    InfluxqlRewrite(#[from] rewrite::Error),

    #[error("must provide only one InfluxQl statement per query")]
    InfluxqlSingleStatement,

    #[error("must specify a 'db' parameter, or provide the database in the InfluxQL query")]
    InfluxqlNoDatabase,

    #[error(
        "provided a database in both the parameters ({param_db}) and \
        query string ({query_db}) that do not match, if providing a query \
        that specifies the database, you can omit the 'database' parameter \

View on GitHub (pinned to 06200ef96b)