{"record":{"id":"815e52c4cda1e77f","repo":"reactiveui/refit","slug":"the-httpresponsemessage-has-no-associated-requestm","errorCode":null,"errorMessage":"The HttpResponseMessage has no associated RequestMessage. When supplying a custom HttpMessageHandler (for example in a test), ensure it sets HttpResponseMessage.RequestMessage.","messagePattern":"The HttpResponseMessage has no associated RequestMessage\\. When supplying a custom HttpMessageHandler \\(for example in a test\\), ensure it sets HttpResponseMessage\\.RequestMessage\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Refit/DefaultApiExceptionFactory.cs","lineNumber":31,"sourceCode":"    /// <param name=\"responseMessage\">The response message.</param>\n    /// <returns>A task that yields the created exception, or null when the response was successful.</returns>\n    public ValueTask<Exception?> CreateAsync(HttpResponseMessage responseMessage) =>\n        responseMessage?.IsSuccessStatusCode == false\n            ? CreateExceptionAsync(responseMessage, refitSettings)\n            : default;\n\n    /// <summary>Builds an <see cref=\"ApiException\"/> for the given unsuccessful response.</summary>\n    /// <param name=\"responseMessage\">The response message.</param>\n    /// <param name=\"refitSettings\">The Refit settings.</param>\n    /// <returns>The created exception.</returns>\n    /// <exception cref=\"InvalidOperationException\"><paramref name=\"responseMessage\"/> has no associated request message, which a custom <see cref=\"HttpMessageHandler\"/> must set itself.</exception>\n    internal static async ValueTask<Exception?> CreateExceptionAsync(\n        HttpResponseMessage responseMessage,\n        RefitSettings refitSettings)\n    {\n        var requestMessage =\n            responseMessage.RequestMessage\n            ?? throw new InvalidOperationException(\n                \"The HttpResponseMessage has no associated RequestMessage. When supplying a \"\n                + \"custom HttpMessageHandler (for example in a test), ensure it sets \"\n                + \"HttpResponseMessage.RequestMessage.\");\n\n        return await ApiException\n            .Create(requestMessage, requestMessage.Method, responseMessage, refitSettings)\n            .ConfigureAwait(false);\n    }\n}\n","sourceCodeStart":13,"sourceCodeEnd":41,"githubUrl":"https://github.com/reactiveui/refit/blob/b455f65ecc4c97d092317e349cb775f9cfc6bcdf/src/Refit/DefaultApiExceptionFactory.cs#L13-L41","documentation":"Thrown by DefaultApiExceptionFactory.CreateExceptionAsync when an unsuccessful HttpResponseMessage has a null RequestMessage. To build an ApiException Refit needs the originating request (method, headers, URI); a response missing that back-reference cannot be turned into a meaningful error, so the factory refuses rather than emit a misleading exception.","triggerScenarios":"A custom HttpMessageHandler returns an error HttpResponseMessage without setting .RequestMessage, and that response flows into Refit's error path which calls DefaultApiExceptionFactory. Common in test doubles and delegating handlers that construct responses manually.","commonSituations":"Unit/integration tests with a mock handler that does `new HttpResponseMessage(HttpStatusCode.InternalServerError)` and forgets the request link; custom production handlers/interceptors that synthesize responses; some mocking libraries strip the RequestMessage.","solutions":["In your custom/test handler, set response.RequestMessage = request before returning it.","When deriving from DelegatingHandler, call await base.SendAsync(request, ct) so the framework links the request, then modify the returned response rather than building a new one.","If you must create a fresh response, copy request.Method and request.RequestUri and assign RequestMessage explicitly."],"exampleFix":"// before — custom handler returns a detached error response\nprotected override Task<HttpResponseMessage> SendAsync(\n    HttpRequestMessage request, CancellationToken ct)\n    => Task.FromResult(new HttpResponseMessage(HttpStatusCode.InternalServerError));\n\n// after — link the request onto the response\nprotected override Task<HttpResponseMessage> SendAsync(\n    HttpRequestMessage request, CancellationToken ct)\n{\n    var resp = new HttpResponseMessage(HttpStatusCode.InternalServerError);\n    resp.RequestMessage = request;\n    return Task.FromResult(resp);\n}","handlingStrategy":"validation","validationCode":"// In any custom handler, always link the request onto the response.\nstatic HttpResponseMessage Linked(HttpResponseMessage resp, HttpRequestMessage req)\n{\n    resp.RequestMessage = req;\n    return resp;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always set response.RequestMessage in custom/test HttpMessageHandlers.","Prefer DelegatingHandler + base.SendAsync so the framework links the request automatically.","Add a shared test helper that asserts RequestMessage is non-null on fabricated responses."],"tags":["http","httpmessagehandler","api-exception","request-message","testing"],"backgroundTag":null,"analyzedSha":"b455f65ecc4c97d092317e349cb775f9cfc6bcdf","analyzedAt":"2026-08-13T21:20:57.878Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}