-
-
Notifications
You must be signed in to change notification settings - Fork 0
Responses and Errors
Every Restling operation returns RestRequestResult or RestRequestResult<T>. The typed result inherits all untyped metadata and adds Data.
| Property | Description |
|---|---|
IsSuccessful |
Whether no exception occurred and the HTTP status is recognized as successful. |
StatusCode |
Nullable HTTP status code. It can be null when no response was received. |
Data |
Deserialized value on RestRequestResult<T>. |
Content |
Response content represented as a string. Binary data is represented as Base64. |
RawContent |
Original response body bytes. |
RetrievedContent |
Decoded content, binary flag, and parsed media type. |
ContentType |
Response media type. |
CharSet |
Parsed response charset. |
Elapsed |
Request execution duration. |
ResponseHeaders |
Read-only response headers and redirect location. |
Exception |
Execution or decoding exception captured by Restling. |
Request |
The RestRequest associated with the result. |
RestRequestResult<Customer> result = await client.GetAsync<Customer>(uri,
cancellationToken: cancellationToken);
if (!result.IsSuccessful)
{
logger.LogWarning("Request failed with status {StatusCode}: {Error}",
result.StatusCode,
result.Exception?.Message);
return;
}
if (result.Data is null)
{
logger.LogWarning("The response was successful but contained no customer data.");
return;
}
Customer customer = result.Data;Do not assume that StatusCode or Data is present. Network failures can produce an exception without an HTTP status, and a successful response may have no body.
IsSuccessful currently recognizes these values:
- 200 OK
- 201 Created
- 202 Accepted
- 203 Non-Authoritative Information
- 204 No Content
- 205 Reset Content
- 206 Partial Content
- 207 Multi-Status
- 208 Already Reported
- 226 IM Used
Other status codes return IsSuccessful == false, even when the server's semantics treat them specially.
IEnumerable<string>? values;
if (result.ResponseHeaders.Headers.TryGetValue("ETag", out values))
{
Console.WriteLine(string.Join(", ", values));
}
Console.WriteLine(result.ResponseHeaders.RedirectLocation);The default SocketsHttpHandler disables automatic redirects, allowing the caller to inspect RedirectLocation. You can enable redirects through Client Configuration.
Pass a CancellationToken to every async call. Treat cancellation distinctly when your application needs separate user feedback; inspect Exception for the captured cancellation error.