reactiveui/refit · error · ArgumentException
Response must have an associated request message.
Error message
Response must have an associated request message.
What it means
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.
Source
Thrown at src/Refit/ApiResponse{T}.cs:58
RefitSettings settings)
: this(response, content, settings, null)
{
}
/// <summary>Initializes a new instance of the <see cref="ApiResponse{T}"/> class.</summary>
/// <param name="response">Original HTTP Response message.</param>
/// <param name="content">Response content.</param>
/// <param name="settings">Refit settings used to send the request.</param>
/// <param name="error">The exception, if the request failed.</param>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="response"/> is <c>null</c>.</exception>
public ApiResponse(
HttpResponseMessage response,
T? content,
RefitSettings settings,
ApiExceptionBase? error)
: this(
(response ?? throw new ArgumentNullException(nameof(response))).RequestMessage
?? throw new ArgumentException(
"Response must have an associated request message.",
nameof(response)),
response,
content,
settings,
error)
{
}
/// <summary>Gets the deserialized request content as <typeparamref name="T"/>.</summary>
public T? Content { get; } = content;
/// <summary>Gets a value indicating whether deserialized <see cref="Content"/> is available.</summary>
[MemberNotNullWhen(true, nameof(Content))]
public bool HasContent => Content is not null;
/// <summary>Gets a value indicating whether the request was successful and deserialized <see cref="Content"/> is available.</summary>
[MemberNotNullWhen(true, nameof(Content))]View on GitHub (pinned to b455f65ecc)
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.
Example fix
// before
var resp = new HttpResponseMessage(HttpStatusCode.OK) { Content = ... };
var api = new ApiResponse<MyDto>(resp, dto, settings, null); // throws
// after
var resp = new HttpResponseMessage(HttpStatusCode.OK) { Content = ... };
resp.RequestMessage = requestMsg;
var api = new ApiResponse<MyDto>(resp, dto, settings, null); Defensive patterns
Strategy: validation
Validate before calling
// Ensure any manually constructed response carries its request.
static HttpResponseMessage WithRequest(HttpResponseMessage resp, HttpRequestMessage req)
{
resp.RequestMessage = req ?? throw new ArgumentNullException(nameof(req));
return resp;
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- The HttpResponseMessage has no associated RequestMessage. Wh
- URL path {relativePath} must not contain CR or LF characters
- Response is successful, cannot create an ApiException.
- The response is unavailable for this API response.
- The [Url] parameter value "{value}" must be an absolute URI
AI-assisted analysis of reactiveui/refit@b455f65ecc (2026-08-13).
Data as JSON: /api/errors/85ee6c3e138c627e.
Report an issue: GitHub.