{"record":{"id":"58fface52520cf71","repo":"microsoft/aspire","slug":"http-command-request-content-type-cannot-be-specified","errorCode":null,"errorMessage":"HTTP command request content type cannot be specified without request content.","messagePattern":"HTTP command request content type cannot be specified without request content\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting/ResourceBuilderExtensions.cs","lineNumber":3854,"sourceCode":"        if (requestData is null)\n        {\n            throw new InvalidOperationException(\"The HTTP command prepare-request callback returned null.\");\n        }\n\n        if (!string.IsNullOrWhiteSpace(requestData.MethodName))\n        {\n            request.Method = new HttpMethod(requestData.MethodName);\n        }\n\n        if (requestData.Content is not null)\n        {\n            request.Content = !string.IsNullOrWhiteSpace(requestData.ContentType)\n                ? new StringContent(requestData.Content, Encoding.UTF8, requestData.ContentType)\n                : new StringContent(requestData.Content, Encoding.UTF8);\n        }\n        else if (!string.IsNullOrWhiteSpace(requestData.ContentType))\n        {\n            throw new InvalidOperationException(\"HTTP command request content type cannot be specified without request content.\");\n        }\n\n        if (requestData.Headers is null)\n        {\n            return;\n        }\n\n        foreach (var (name, value) in requestData.Headers)\n        {\n            if (!request.Headers.TryAddWithoutValidation(name, value) &&\n                request.Content?.Headers.TryAddWithoutValidation(name, value) != true)\n            {\n                throw new InvalidOperationException($\"HTTP command request header '{name}' could not be applied.\");\n            }\n        }\n    }\n\n    internal static async Task<ExecuteCommandResult> GetDefaultHttpCommandResultAsync(HttpResponseMessage response, HttpCommandOptions commandOptions, CancellationToken cancellationToken)","sourceCodeStart":3836,"sourceCodeEnd":3872,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting/ResourceBuilderExtensions.cs#L3836-L3872","documentation":"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.","triggerScenarios":"HttpCommandRequestExportData returned from the prepare-request callback with ContentType set (e.g. \"application/json\") but Content null or whitespace.","commonSituations":"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.","solutions":["Set Content to the serialized body string whenever ContentType is specified","If the request must be body-less, remove ContentType from the export data","If the body may be empty legitimately, pass string.Empty and a valid ContentType only when truly sending content"],"exampleFix":"// before\nnew HttpCommandRequestExportData { MethodName = \"POST\", Url = url, ContentType = \"application/json\" } // no Content\n// after\nnew HttpCommandRequestExportData { MethodName = \"POST\", Url = url, ContentType = \"application/json\", Content = JsonSerializer.Serialize(payload) }","handlingStrategy":"validation","validationCode":"if (!string.IsNullOrWhiteSpace(requestData.ContentType) && string.IsNullOrWhiteSpace(requestData.Content))\n    throw new InvalidOperationException(\"ContentType without Content\");","typeGuard":"bool HasContentTypeWithoutContent(HttpCommandRequestExportData d) =>\n    !string.IsNullOrWhiteSpace(d.ContentType) && string.IsNullOrWhiteSpace(d.Content);","tryCatchPattern":"try { /* execute command */ }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"content type cannot be specified without request content\")) { /* drop ContentType or supply body */ }","preventionTips":["Always set Content together with ContentType","Treat ContentType as part of the body contract, not a standalone header"],"tags":["http","request-content","content-type","validation","aspire-hosting"],"backgroundTag":"conflicting-config-options","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T09:17:21.228Z"}