Skip to content

Consume result

After Execute request returns an IHttpRequestResult, consumers convert its Content and Headers into strongly typed values using GetResult<T>, GetContent<T>, and GetHeaderValue<T>.

Field Value
Operation ID OP-05
Trigger A consumer calls GetResult<T>()/GetResult<T>(CultureInfo), GetContent<T>()/GetContent<T>(CultureInfo), or GetHeaderValue<T>(name) on an IHttpRequestResult
Inputs An IHttpRequestResult (status, content, headers) and a target type T; optional CultureInfo
Outputs A converted T value, default(T) (for GetResult<T> on an unsuccessful result), or the converted header value
Primary components HttpRequestResult.GetResult<T>, HttpRequestResult.GetContent<T>, HttpRequestResult.GetHeaderValue<T>, HttpJsonClientExtensions.ToReturnType<T>, HttpJsonClientExtensions.GetHeaderValue<T>
Entry point src/SAIF.NetCore.HttpClients/SAIF.NetCore.HttpClients/SAIF.NetCore.HttpClients/HttpJsonClient/HttpRequestResult.cs

Steps

  1. GetResult<T>(): checks IsSuccessful.
  2. If false: return default(T) without attempting conversion (BR-HTTP-18)
  3. If true: delegate to ToReturnType<T> on Content (step 2).
  4. GetContent<T>(): delegates to ToReturnType<T> on Content regardless of IsSuccessful (BR-HTTP-19)
  5. If T is string and the content both starts and ends with ": strip exactly that outer quote pair (preserving any inner escaped quotes), then convert the unwrapped text with Convert.ChangeType using the supplied (or default) culture.
  6. Otherwise, if T is a value type or string: convert the unmodified content with Convert.ChangeType using the supplied (or default) culture — a quoted numeric string such as "5" is passed through unquoted only when T is string, so this path does not unquote it for a numeric T.
  7. Otherwise: deserialize the unmodified content as JSON using JsonConvert.DeserializeObject with the culture passed through the serializer settings.
  8. GetHeaderValue<T>(name): delegates to the extension method of the same name on Headers.
  9. Look up name case-insensitively using SingleOrDefault (BR-HTTP-20)
  10. On failure: if more than one header matches case-insensitively (a case-variant duplicate), the lookup throws.
  11. If the match is null, ConvertHeaderValue returns default(T).
  12. If T is Nullable<U>, unwrap to the underlying type U before conversion.

Process flow

The following diagram visualizes the three result-consumption paths.

flowchart TD
    Start([Consumer holds IHttpRequestResult]) --> Choice{Which accessor?}
    Choice -->|GetResult&lt;T&gt;| CheckSuccess{IsSuccessful?}
    CheckSuccess -->|No| Default(["Return default(T)"])
    CheckSuccess -->|Yes| Convert["ToReturnType&lt;T&gt;(Content)"]
    Choice -->|GetContent&lt;T&gt;| Convert
    Convert --> StringQuoted{"T is string and content<br>starts/ends with &quot;?"}
    StringQuoted -->|Yes| UnwrapQuotes["Strip outer quote pair<br>(preserve inner escaped quotes)"]
    StringQuoted -->|No| TypeCheck{"Value type or string?"}
    UnwrapQuotes --> ChangeType["Convert.ChangeType(culture)"]
    TypeCheck -->|Yes| ChangeType
    TypeCheck -->|No| Json["JsonConvert.DeserializeObject(culture)"]
    ChangeType --> Done(["Typed value returned"])
    Json --> Done
    Choice -->|GetHeaderValue&lt;T&gt;| Lookup["Case-insensitive SingleOrDefault lookup"]
    Lookup --> Dup{"Case-variant duplicate?"}
    Dup -->|Yes| Throw(["Throws"])
    Dup -->|No| Null{"Match is null?"}
    Null -->|Yes| DefaultHeader(["Return default(T)"])
    Null -->|No| UnwrapHeader["Unwrap Nullable&lt;T&gt; if needed, convert"]
    UnwrapHeader --> DoneHeader(["Typed value returned"])

Failure and edge-case behavior

  • GetResult<T> never attempts conversion for an unsuccessful response, even if Content contains valid data for T; use GetContent<T> to force conversion regardless of status (BR-HTTP-18)
  • A case-variant duplicate header name (for example both Content-Type and content-type present) causes GetHeaderValue<T> to throw rather than silently pick one.
  • Content that is not valid JSON for a non-value-type T surfaces a JsonConvert deserialization exception to the caller.

Sources

  • src/SAIF.NetCore.HttpClients/SAIF.NetCore.HttpClients/SAIF.NetCore.HttpClients/HttpJsonClient/HttpRequestResult.cs
  • src/SAIF.NetCore.HttpClients/SAIF.NetCore.HttpClients/SAIF.NetCore.HttpClients/HttpJsonClient/HttpJsonClientExtensions.cs