reactiveui/refit · error · ArgumentException

Unexpected parameter type in a Multipart request. Parameter

Error message

Unexpected parameter type in a Multipart request. Parameter {fileName} is of type {parameterType}, whereas allowed types are {allowedTypes}

What it means

While building a multipart/form-data request body, Refit attempts to serialize each part with the configured content serializer and, on failure, falls back to an allowlist of multipart-native types (String, Stream, FileInfo, byte[], or anything JSON-serializable). This throw is the fallback: the part value is none of those and serialization also failed, with the original exception attached as inner.

Source

Thrown at src/Refit.Reflection/RequestBuilderImplementation.Payload.cs:369

        // Fallback to serializer
        Exception e;
        try
        {
            multiPartContent.Add(
                _settings.ContentSerializer.ToHttpContent(itemValue),
                parameterName);
            return;
        }
        catch (Exception ex)
        {
            // Eat this since we're about to throw as a fallback anyway
            e = ex;
        }

        const string allowedTypes = "String, Stream, FileInfo, Byte array and anything that's JSON serializable";
        var parameterType = itemValue.GetType().Name;
        throw new ArgumentException(
            $"Unexpected parameter type in a Multipart request. Parameter {fileName} is of type {parameterType}, whereas allowed types are {allowedTypes}",
            nameof(itemValue),
            e);
    }

    /// <summary>Appends query key/value pairs for a single parameter value.</summary>
    /// <param name="queryParamsToAdd">The list receiving query parameters.</param>
    /// <param name="param">The parameter value.</param>
    /// <param name="parameterInfo">Reflection info for the parameter.</param>
    /// <param name="queryPath">The query key path for the parameter.</param>
    /// <param name="queryAttribute">The query attribute governing formatting.</param>
    internal void AppendQueryParameter(
        List<QueryParameterEntry> queryParamsToAdd,
        object? param,
        ParameterInfo parameterInfo,
        string queryPath,
        QueryAttribute queryAttribute)
    {

View on GitHub (pinned to b455f65ecc)

Solutions

  1. Inspect the inner exception 'e' to see the exact serialization failure and fix that (add a parameterless constructor, resolve circular references, etc.).
  2. Convert the part to a supported multipart type: serialize to a string/byte[] yourself, or pass a Stream/FileInfo for file parts.
  3. Ensure the content serializer configured in RefitSettings can handle the part's type.
  4. For complex objects meant as JSON parts, confirm they are plain serializable POCOs.

Example fix

// before
[Multipart]
[Post("/upload")]
Task UploadAsync([AliasAs("meta")] SomeUnserializableType meta);

// after (serialize to a string part you control)
[Multipart]
[Post("/upload")]
Task UploadAsync([AliasAs("meta")] string metaJson);
// caller: await api.UploadAsync(JsonConvert.SerializeObject(meta));
Defensive patterns

Strategy: type-guard

Validate before calling

static bool IsMultipartAllowed(object? value) {
    if (value is null) return true;
    var t = value.GetType();
    return value is string or Stream or System.IO.FileInfo
        || t == typeof(byte[])
        || (t.IsClass && t.GetConstructor(Type.EmptyTypes) is not null); // crude JSON-serializable check
}

Type guard

static bool IsMultipartPart(object value) =>
    value is string or Stream or System.IO.FileInfo or byte[];

Prevention

When it happens

Trigger: A [Multipart] method parameter value is a type that is neither a multipart-native type nor serializable by the content serializer, e.g. an anonymous object, a Type instance, a delegate, or a POCO whose serializer threw (missing parameterless constructor, circular reference).

Common situations: Passing a complex DTO the serializer cannot handle; a property whose JSON serialization fails (circular refs, missing ctor); passing an unsupported collection as a part; serializer misconfigured for the payload.

Related errors


AI-assisted analysis of reactiveui/refit@b455f65ecc (2026-08-13). Data as JSON: /api/errors/9b12ee0f11278098. Report an issue: GitHub.