{"record":{"id":"985376d99d72ed48","repo":"microsoft/aspire","slug":"the-http-command-prepare-request-callback-returned-null","errorCode":null,"errorMessage":"The HTTP command prepare-request callback returned null.","messagePattern":"The HTTP command prepare-request callback returned null\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting/ResourceBuilderExtensions.cs","lineNumber":3838,"sourceCode":"                {\n                    ResourceName = context.ResourceName,\n                    Endpoint = context.Endpoint,\n                    CancellationToken = context.CancellationToken,\n                    Arguments = context.Arguments\n                }).ConfigureAwait(false);\n\n                ApplyHttpCommandRequestExportData(context.Request, requestData);\n            };\n        }\n\n        return commandOptions;\n    }\n\n    private static void ApplyHttpCommandRequestExportData(HttpRequestMessage request, HttpCommandRequestExportData requestData)\n    {\n        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","sourceCodeStart":3820,"sourceCodeEnd":3856,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting/ResourceBuilderExtensions.cs#L3820-L3856","documentation":"The HTTP command's prepare-request callback is expected to return a populated export-data object describing the request (method, content, headers). If the callback returns null Aspire cannot build the HttpRequestMessage and throws InvalidOperationException.","triggerScenarios":"Supplying an HttpCommandOptions.PrepareRequest callback (Func that maps a command context to HttpCommandRequestExportData) whose body returns null — e.g. an early-return path, or forgetting the return statement.","commonSituations":"Conditional prepare-request logic that skips returning data when a header/value is missing; refactoring that replaced the returned object with a local variable; copying a callback signature but not implementing the return.","solutions":["Ensure the prepare-request callback always returns a non-null HttpCommandRequestExportData instance","Add a default return (e.g. new HttpCommandRequestExportData { MethodName = \"POST\", Url = ... }) for all code paths","Validate the callback's inputs before returning and throw a descriptive exception if inputs are invalid instead of returning null"],"exampleFix":"// before\nrequest => condition ? new HttpCommandRequestExportData { ... } : null\n// after\nrequest => new HttpCommandRequestExportData { MethodName = condition ? \"PUT\" : \"POST\", Url = url, ... }","handlingStrategy":"type-guard","validationCode":"HttpCommandRequestExportData Prepare(...) {\n  var data = BuildRequestData();\n  return data ?? throw new InvalidOperationException(\"prepare-request produced null\");\n}","typeGuard":"bool PreparedNotNull(HttpCommandRequestExportData? d) => d is not null;","tryCatchPattern":"try { builder.WithHttpCommand(o => o.PrepareRequest = cb); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"prepare-request callback returned null\")) { /* fix callback return paths */ }","preventionTips":["Ensure every code path of the prepare-request callback returns a value","Enable nullable reference types so a null return is flagged at compile time"],"tags":["http","callback","null-return","aspire-hosting"],"backgroundTag":"null-argument","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}