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¶
GetResult<T>(): checksIsSuccessful.- If
false: returndefault(T)without attempting conversion (BR-HTTP-18) - If
true: delegate toToReturnType<T>onContent(step 2). GetContent<T>(): delegates toToReturnType<T>onContentregardless ofIsSuccessful(BR-HTTP-19)- If
Tisstringand the content both starts and ends with": strip exactly that outer quote pair (preserving any inner escaped quotes), then convert the unwrapped text withConvert.ChangeTypeusing the supplied (or default) culture. - Otherwise, if
Tis a value type orstring: convert the unmodified content withConvert.ChangeTypeusing the supplied (or default) culture — a quoted numeric string such as"5"is passed through unquoted only whenTisstring, so this path does not unquote it for a numericT. - Otherwise: deserialize the unmodified content as JSON using
JsonConvert.DeserializeObjectwith the culture passed through the serializer settings. GetHeaderValue<T>(name): delegates to the extension method of the same name onHeaders.- Look up
namecase-insensitively usingSingleOrDefault(BR-HTTP-20) - On failure: if more than one header matches case-insensitively (a case-variant duplicate), the lookup throws.
- If the match is null,
ConvertHeaderValuereturnsdefault(T). - If
TisNullable<U>, unwrap to the underlying typeUbefore conversion.
Process flow¶
The following diagram visualizes the three result-consumption paths.
flowchart TD
Start([Consumer holds IHttpRequestResult]) --> Choice{Which accessor?}
Choice -->|GetResult<T>| CheckSuccess{IsSuccessful?}
CheckSuccess -->|No| Default(["Return default(T)"])
CheckSuccess -->|Yes| Convert["ToReturnType<T>(Content)"]
Choice -->|GetContent<T>| Convert
Convert --> StringQuoted{"T is string and content<br>starts/ends with "?"}
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<T>| 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<T> if needed, convert"]
UnwrapHeader --> DoneHeader(["Typed value returned"])
Failure and edge-case behavior¶
GetResult<T>never attempts conversion for an unsuccessful response, even ifContentcontains valid data forT; useGetContent<T>to force conversion regardless of status (BR-HTTP-18)- A case-variant duplicate header name (for example both
Content-Typeandcontent-typepresent) causesGetHeaderValue<T>to throw rather than silently pick one. - Content that is not valid JSON for a non-value-type
Tsurfaces aJsonConvertdeserialization exception to the caller.
Related documentation¶
Sources¶
src/SAIF.NetCore.HttpClients/SAIF.NetCore.HttpClients/SAIF.NetCore.HttpClients/HttpJsonClient/HttpRequestResult.cssrc/SAIF.NetCore.HttpClients/SAIF.NetCore.HttpClients/SAIF.NetCore.HttpClients/HttpJsonClient/HttpJsonClientExtensions.cs