reactiveui/refit · error · InvalidOperationException

The HttpResponseMessage has no associated RequestMessage. Wh

Error message

The HttpResponseMessage has no associated RequestMessage. When supplying a custom HttpMessageHandler (for example in a test), ensure it sets HttpResponseMessage.RequestMessage.

What it means

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.

Source

Thrown at src/Refit/DefaultApiExceptionFactory.cs:31

    /// <param name="responseMessage">The response message.</param>
    /// <returns>A task that yields the created exception, or null when the response was successful.</returns>
    public ValueTask<Exception?> CreateAsync(HttpResponseMessage responseMessage) =>
        responseMessage?.IsSuccessStatusCode == false
            ? CreateExceptionAsync(responseMessage, refitSettings)
            : default;

    /// <summary>Builds an <see cref="ApiException"/> for the given unsuccessful response.</summary>
    /// <param name="responseMessage">The response message.</param>
    /// <param name="refitSettings">The Refit settings.</param>
    /// <returns>The created exception.</returns>
    /// <exception cref="InvalidOperationException"><paramref name="responseMessage"/> has no associated request message, which a custom <see cref="HttpMessageHandler"/> must set itself.</exception>
    internal static async ValueTask<Exception?> CreateExceptionAsync(
        HttpResponseMessage responseMessage,
        RefitSettings refitSettings)
    {
        var requestMessage =
            responseMessage.RequestMessage
            ?? throw new InvalidOperationException(
                "The HttpResponseMessage has no associated RequestMessage. When supplying a "
                + "custom HttpMessageHandler (for example in a test), ensure it sets "
                + "HttpResponseMessage.RequestMessage.");

        return await ApiException
            .Create(requestMessage, requestMessage.Method, responseMessage, refitSettings)
            .ConfigureAwait(false);
    }
}

View on GitHub (pinned to b455f65ecc)

Solutions

  1. In your custom/test handler, set response.RequestMessage = request before returning it.
  2. 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.
  3. If you must create a fresh response, copy request.Method and request.RequestUri and assign RequestMessage explicitly.

Example fix

// before — custom handler returns a detached error response
protected override Task<HttpResponseMessage> SendAsync(
    HttpRequestMessage request, CancellationToken ct)
    => Task.FromResult(new HttpResponseMessage(HttpStatusCode.InternalServerError));

// after — link the request onto the response
protected override Task<HttpResponseMessage> SendAsync(
    HttpRequestMessage request, CancellationToken ct)
{
    var resp = new HttpResponseMessage(HttpStatusCode.InternalServerError);
    resp.RequestMessage = request;
    return Task.FromResult(resp);
}
Defensive patterns

Strategy: validation

Validate before calling

// In any custom handler, always link the request onto the response.
static HttpResponseMessage Linked(HttpResponseMessage resp, HttpRequestMessage req)
{
    resp.RequestMessage = req;
    return resp;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of reactiveui/refit@b455f65ecc (2026-08-13). Data as JSON: /api/errors/815e52c4cda1e77f. Report an issue: GitHub.