{"record":{"id":"85ee6c3e138c627e","repo":"reactiveui/refit","slug":"response-must-have-an-associated-request-message","errorCode":null,"errorMessage":"Response must have an associated request message.","messagePattern":"Response must have an associated request message\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Refit/ApiResponse{T}.cs","lineNumber":58,"sourceCode":"        RefitSettings settings)\n        : this(response, content, settings, null)\n    {\n    }\n\n    /// <summary>Initializes a new instance of the <see cref=\"ApiResponse{T}\"/> class.</summary>\n    /// <param name=\"response\">Original HTTP Response message.</param>\n    /// <param name=\"content\">Response content.</param>\n    /// <param name=\"settings\">Refit settings used to send the request.</param>\n    /// <param name=\"error\">The exception, if the request failed.</param>\n    /// <exception cref=\"ArgumentNullException\">Thrown when <paramref name=\"response\"/> is <c>null</c>.</exception>\n    public ApiResponse(\n        HttpResponseMessage response,\n        T? content,\n        RefitSettings settings,\n        ApiExceptionBase? error)\n        : this(\n            (response ?? throw new ArgumentNullException(nameof(response))).RequestMessage\n            ?? throw new ArgumentException(\n                \"Response must have an associated request message.\",\n                nameof(response)),\n            response,\n            content,\n            settings,\n            error)\n    {\n    }\n\n    /// <summary>Gets the deserialized request content as <typeparamref name=\"T\"/>.</summary>\n    public T? Content { get; } = content;\n\n    /// <summary>Gets a value indicating whether deserialized <see cref=\"Content\"/> is available.</summary>\n    [MemberNotNullWhen(true, nameof(Content))]\n    public bool HasContent => Content is not null;\n\n    /// <summary>Gets a value indicating whether the request was successful and deserialized <see cref=\"Content\"/> is available.</summary>\n    [MemberNotNullWhen(true, nameof(Content))]","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/reactiveui/refit/blob/b455f65ecc4c97d092317e349cb775f9cfc6bcdf/src/Refit/ApiResponse{T}.cs#L40-L76","documentation":"Thrown by the ApiResponse<T> constructor when the supplied HttpResponseMessage is non-null but its RequestMessage property is null. ApiResponse requires a request to be associated (it exposes RequestMessage and uses it for error construction), so a response detached from its request is rejected.","triggerScenarios":"Constructing `new ApiResponse<T>(response, ...)` where response.RequestMessage was never set — most commonly a hand-built HttpResponseMessage in a test or a custom HttpMessageHandler that creates responses without linking them to the request.","commonSituations":"Unit tests that new up HttpResponseMessage(OK) without setting .RequestMessage; custom handlers/delegating handlers that return fresh responses; mocking frameworks that produce responses missing the back-reference.","solutions":["Set response.RequestMessage = request on any HttpResponseMessage you construct before passing it to ApiResponse.","In a custom DelegatingHandler, always set RequestMessage on the response you return (or call base.SendAsync which sets it).","In tests, use a helper that clones the request reference onto the response."],"exampleFix":"// before\nvar resp = new HttpResponseMessage(HttpStatusCode.OK) { Content = ... };\nvar api = new ApiResponse<MyDto>(resp, dto, settings, null); // throws\n\n// after\nvar resp = new HttpResponseMessage(HttpStatusCode.OK) { Content = ... };\nresp.RequestMessage = requestMsg;\nvar api = new ApiResponse<MyDto>(resp, dto, settings, null);","handlingStrategy":"validation","validationCode":"// Ensure any manually constructed response carries its request.\nstatic HttpResponseMessage WithRequest(HttpResponseMessage resp, HttpRequestMessage req)\n{\n    resp.RequestMessage = req ?? throw new ArgumentNullException(nameof(req));\n    return resp;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never construct an HttpResponseMessage for tests/handlers without setting RequestMessage.","In DelegatingHandler, prefer base.SendAsync and mutate the returned response.","Add a test helper that asserts response.RequestMessage is non-null for every fabricated response."],"tags":["api-response","http","request-message","validation"],"backgroundTag":null,"analyzedSha":"b455f65ecc4c97d092317e349cb775f9cfc6bcdf","analyzedAt":"2026-08-13T21:20:57.878Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}