{"record":{"id":"1636895a655397d4","repo":"reactiveui/refit","slug":"content-must-be-an-application-problem-json-comp","errorCode":null,"errorMessage":"Content must be an 'application/problem+json' compliant json string.","messagePattern":"Content must be an 'application/problem\\+json' compliant json string\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Refit/ValidationApiException.cs","lineNumber":122,"sourceCode":"            .ConfigureAwait(false);\n\n        return ex;\n    }\n\n    /// <summary>Validates the exception content and builds the base validation exception.</summary>\n    /// <param name=\"exception\">The API exception to convert.</param>\n    /// <returns>A new validation exception wrapping the API exception.</returns>\n    /// <exception cref=\"ArgumentException\">The content of <paramref name=\"exception\"/> is null, empty, or whitespace, so it cannot be an 'application/problem+json' payload.</exception>\n    internal static ValidationApiException CreateCore(ApiException exception)\n    {\n        ArgumentExceptionHelper.ThrowIfNull(exception);\n\n#if NET8_0_OR_GREATER\n        ArgumentException.ThrowIfNullOrWhiteSpace(exception.Content);\n#else\n        if (string.IsNullOrWhiteSpace(exception.Content))\n        {\n            throw new ArgumentException(\n                \"Content must be an 'application/problem+json' compliant json string.\");\n        }\n#endif\n\n        return new(exception);\n    }\n\n    /// <summary>Deserializes RFC 7807 problem details without requiring public setters on extension data.</summary>\n    /// <param name=\"content\">The JSON problem details content.</param>\n    /// <returns>The deserialized problem details.</returns>\n    /// <exception cref=\"JsonException\"><paramref name=\"content\"/> is not valid JSON, or its root value is not an object.</exception>\n    internal static ProblemDetails DeserializeProblemDetails(string content)\n    {\n#if NET10_0_OR_GREATER\n        var rootElement = JsonElement.Parse(content);\n#else\n        using var document = JsonDocument.Parse(content);\n        var rootElement = document.RootElement;","sourceCodeStart":104,"sourceCodeEnd":140,"githubUrl":"https://github.com/reactiveui/refit/blob/b455f65ecc4c97d092317e349cb775f9cfc6bcdf/src/Refit/ValidationApiException.cs#L104-L140","documentation":"ValidationApiException.CreateCore throws ArgumentException when the source ApiException.Content is null, empty, or whitespace, because an RFC 7807 problem-details payload cannot be parsed from blank content. On .NET 8+ it uses ArgumentException.ThrowIfNullOrWhiteSpace; otherwise it checks manually.","triggerScenarios":"Calling ApiException.AsValidation() (which routes to CreateCore) on an exception whose HTTP response had no body, or whose body was empty/whitespace (e.g. a 400 with no content, or a server that returns headers only).","commonSituations":"Treating every non-2xx as a validation error without first checking for a body; a gateway/proxy stripping the body; a server returning 400 with an empty response for non-validation errors; networking layer truncating the response.","solutions":["Check exception.Content is non-blank before calling AsValidation()","Verify the response Content-Type is application/problem+json before converting","Handle the case where the error is not an RFC 7807 problem (use ApiException directly)","Inspect the status code and only convert known validation status codes (e.g. 400/422) when content is present"],"exampleFix":"// before\ntry { await api.CreateAsync(payload); }\ncatch (ApiException ex) { var validation = ex.AsValidation(); } // throws if content is empty\n\n// after\ntry { await api.CreateAsync(payload); }\ncatch (ApiException ex)\n{\n    if (!string.IsNullOrWhiteSpace(ex.Content)\n        && ex.Headers.ContentType?.MediaType == \"application/problem+json\")\n    {\n        var validation = ex.AsValidation();\n        // handle validation errors\n    }\n    else { throw; }\n}","handlingStrategy":"validation","validationCode":"static bool IsProblemDetailsCandidate(ApiException ex) =>\n    !string.IsNullOrWhiteSpace(ex.Content)\n    && ex.Headers.ContentType?.MediaType == \"application/problem+json\";\n\nif (IsProblemDetailsCandidate(ex))\n    var validation = ex.AsValidation();","typeGuard":"static bool HasProblemContent(ApiException ex) =>\n    !string.IsNullOrWhiteSpace(ex.Content);","tryCatchPattern":"try { var validation = ex.AsValidation(); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"application/problem+json\"))\n{\n    // no body to parse; handle as a generic ApiException\n}","preventionTips":["Check exception.Content is non-blank before calling AsValidation()","Verify the Content-Type is application/problem+json before converting","Only convert known validation status codes (400/422) when content is present","Fall back to ApiException when the body is not an RFC 7807 payload"],"tags":["validation","rfc7807","problem-details","argument","content"],"backgroundTag":null,"analyzedSha":"b455f65ecc4c97d092317e349cb775f9cfc6bcdf","analyzedAt":"2026-08-13T21:20:57.878Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}