microsoft/aspire · error · InvalidOperationException

HTTP command request content type cannot be specified…

Error message

HTTP command request content type cannot be specified without request content.

What it means

In the HTTP command request-export data, a ContentType is only meaningful together with request body content. If ContentType is set while Content is null/empty, Aspire throws InvalidOperationException because it cannot attach a content-type header to a request with no body.

Solutions

  1. Set Content to the serialized body string whenever ContentType is specified
  2. If the request must be body-less, remove ContentType from the export data
  3. If the body may be empty legitimately, pass string.Empty and a valid ContentType only when truly sending content

Example fix

// before
new HttpCommandRequestExportData { MethodName = "POST", Url = url, ContentType = "application/json" } // no Content
// after
new HttpCommandRequestExportData { MethodName = "POST", Url = url, ContentType = "application/json", Content = JsonSerializer.Serialize(payload) }
Defensive patterns

Strategy: validation

Validate before calling

if (!string.IsNullOrWhiteSpace(requestData.ContentType) && string.IsNullOrWhiteSpace(requestData.Content))
    throw new InvalidOperationException("ContentType without Content");

Type guard

bool HasContentTypeWithoutContent(HttpCommandRequestExportData d) =>
    !string.IsNullOrWhiteSpace(d.ContentType) && string.IsNullOrWhiteSpace(d.Content);

Try / catch

try { /* execute command */ }
catch (InvalidOperationException ex) when (ex.Message.Contains("content type cannot be specified without request content")) { /* drop ContentType or supply body */ }

Prevention

When it happens

Trigger: HttpCommandRequestExportData returned from the prepare-request callback with ContentType set (e.g. "application/json") but Content null or whitespace.

Common situations: Copy-pasting request setup that sets ContentType but never assigns the body; conditional body-building logic that only sometimes fills Content; assuming a default empty body is created automatically.

Understand the failure class

Background: Conflicting config options: "cannot be used together" — configuration validation errors across open-source libraries — this error's family across 162 libraries.

Related errors


AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16). Data as JSON: /api/errors/58fface52520cf71. Report an issue: GitHub.

Appendix: source

Thrown at src/Aspire.Hosting/ResourceBuilderExtensions.cs:3854

        if (requestData is null)
        {
            throw new InvalidOperationException("The HTTP command prepare-request callback returned null.");
        }

        if (!string.IsNullOrWhiteSpace(requestData.MethodName))
        {
            request.Method = new HttpMethod(requestData.MethodName);
        }

        if (requestData.Content is not null)
        {
            request.Content = !string.IsNullOrWhiteSpace(requestData.ContentType)
                ? new StringContent(requestData.Content, Encoding.UTF8, requestData.ContentType)
                : new StringContent(requestData.Content, Encoding.UTF8);
        }
        else if (!string.IsNullOrWhiteSpace(requestData.ContentType))
        {
            throw new InvalidOperationException("HTTP command request content type cannot be specified without request content.");
        }

        if (requestData.Headers is null)
        {
            return;
        }

        foreach (var (name, value) in requestData.Headers)
        {
            if (!request.Headers.TryAddWithoutValidation(name, value) &&
                request.Content?.Headers.TryAddWithoutValidation(name, value) != true)
            {
                throw new InvalidOperationException($"HTTP command request header '{name}' could not be applied.");
            }
        }
    }

    internal static async Task<ExecuteCommandResult> GetDefaultHttpCommandResultAsync(HttpResponseMessage response, HttpCommandOptions commandOptions, CancellationToken cancellationToken)

View on GitHub (pinned to 25830f84bd)