influxdata/influxdb · error · Error

failed to serialize the request body

Error message

failed to serialize the request body: {0}

What it means

Error::RequestSerialization wraps a serde_json::Error raised while serializing the outgoing request body to JSON before sending. The client cannot produce a valid JSON payload from the value supplied by the caller.

Solutions

  1. Fix the payload type so it is JSON-serializable (string keys, JSON-compatible numeric types).
  2. Serialize the body locally with serde_json::to_string first in debug flows to surface the exact serde error.
  3. Update payload structs to match the client's expected API types after version upgrades.

Example fix

// before
let body = std::collections::HashMap<i32, String>; // non-string keys
client.post(body).await?;
// after
let body = std::collections::HashMap<String, String>;
client.post(body).await?;
Defensive patterns

Strategy: validation

Validate before calling

// verify payload serializes before sending
fn ensure_json<T: serde::Serialize>(v: &T) -> Result<(), serde_json::Error> {
    serde_json::to_string(v).map(|_| ())
}

Try / catch

match serde_json::to_string(&body) {
    Ok(s) => send(s).await,
    Err(e) => Err(anyhow!("request body not serializable: {e}")),
}

Prevention

When it happens

Trigger: Calling write/metadata APIs with a body struct or value that serde_json cannot serialize (e.g. a map with non-string keys, NaN/f64 values in strict contexts, or a type that fails custom serialization).

Common situations: Hand-built line-protocol/JSON payloads with wrong field types after a struct change; dynamic serde_json::json! values containing unsupported types; version drift between client and payload types.

Understand the failure class

Background: "JSON serialization failed", "not JSON serializable", "Failed to serialize": why JSON marshaling errors happen and how to fix them — this error's family across 46 libraries.

Related errors


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

Appendix: source

Thrown at influxdb3_client/src/lib.rs:34

use std::{fmt::Display, num::NonZeroUsize, path::PathBuf, string::FromUtf8Error, time::Duration};
use url::Url;

use influxdb3_types::http::*;
pub use influxdb3_types::write::Precision;

/// Primary error type for the [`Client`]
#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("base URL error: {}", reqwest_description(.0))]
    BaseUrl(#[source] reqwest::Error),

    #[error("request URL error: {0}")]
    RequestUrl(#[from] url::ParseError),

    #[error("failed to read the API response bytes: {}", reqwest_description(.0))]
    Bytes(#[source] reqwest::Error),

    #[error("failed to serialize the request body: {0}")]
    RequestSerialization(#[source] serde_json::Error),

    #[error(
        "provided parameter ('{name}') could not be converted \
        to a statment parameter"
    )]
    ConvertQueryParam {
        name: String,
        #[source]
        source: iox_query_params::Error,
    },

    #[error("invalid UTF8 in response: {0}")]
    InvalidUtf8(#[from] FromUtf8Error),

    #[error("failed to parse JSON response: {}", reqwest_description(.0))]
    Json(#[source] reqwest::Error),

View on GitHub (pinned to 06200ef96b)