Skip to content

AddJson on CapturedMultipartContent not using JsonSerializerOptions configured at startup. #863

@BraveThrasher

Description

@BraveThrasher

I'm using the IFlurlClientBuilder clientless pattern at startup to configure json serialization as follows.

FlurlHttp.Clients.WithDefaults(builder => builder
				 .WithSettings(httpSettings =>
				 {
					 var options = new System.Text.Json.JsonSerializerOptions()
					 {
						 PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseUpper,
						 WriteIndented = true,
					 };
					 options.Converters.Add(new JsonStringEnumConverter());
					 httpSettings.JsonSerializer = new DefaultJsonSerializer(options);
				 }));

When executing a call to an api to upload some files with some metadata (as follows) the metadata is not being serialized according to the configured JsonSerializerOptions.

public async Task Upload(IDictionary<Guid, Stream> files, IDictionary<Guid, MetaData> metaData, Guid packageId)
{
	try
	{
		var uri = new System.Uri("https://subdomain.domain.com/api/");
		var response = await uri.WithHeader("apikey", "apikey-redacted")
								.AppendPathSegments("upload")
								.PostMultipartAsync(mp => 
									{
										foreach (var file in files)
										{
											file.Value.Position = 0;
											mp.AddFile("files", file.Value, metaData[file.Key].FileName);
										}
										mp.AddJson("metaData", metaData.Values);
										mp.AddString("packageId", packageId.ToString());
									});
	}
	catch (FlurlHttpException ex)
	{
		var err = await ex.GetResponseStringAsync();
		logger.LogError("Error while uploading: {message}", err);
	}

}

Expected is:

[{
        "ID": "c79299d2-5e6a-4bbf-b28e-268170f5555a",
        "FILE_NAME": "File_c79299d2-5e6a-4bbf-b28e-268170f5555a.pdf",
        "META_DATA": {
            "CATEGORY": "AA",
            "Year": 2025,
            "MONTH": 10,
            "DAY": 9,
            "COMPANY": "Company XYZ",
            "ACCOUNT_ID": "455"
        }
    }, {
        "ID": "4a0b4465-e31b-44d3-8579-61ce1071366d",
        "FILE_NAME": "File_4a0b4465-e31b-44d3-8579-61ce1071366d.pdf",
        "META_DATA": {
            "CATEGORY": "AA",
            "YEAR": 2025,
            "MONTH": 10,
            "DAY": 9,
            "COMPANY": "Company ABC",
            "ACCOUNT_ID": "490"
        }
    }
]

However actual result is:

[{"Id":"c79299d2-5e6a-4bbf-b28e-268170f5555a","FileName":"File_c79299d2-5e6a-4bbf-b28e-268170f5555a.pdf","MetaData":{"Category":"AA","Year":2025,"Month":10,"Day":9,"Company":"Company XYZ","AccountId":"455"}},{"Id":"4a0b4465-e31b-44d3-8579-61ce1071366d","FileName":"File_4a0b4465-e31b-44d3-8579-61ce1071366d.pdf","MetaData":{"Category":"AA","Year":2025,"Month":10,"Day":9,"Company":"Company ABC","AccountId":"490"}}]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions