Skip to content

HttpClient Extension Methods

Dennis C. Mitchell edited this page Mar 28, 2019 · 4 revisions

In its HttpClient class and extension methods (from the Microsoft.AspNet.WebApi.Client), Microsoft has created some simple and powerful methods for sending HTTP requests and handling the responses. For many scenarios, the Microsoft-provided methods may be sufficient. EDennis.AspNetCore.Base adds some HttpClient extension methods to facilitate forwarding requests from a parent HTTP request and to facilitate creating return values that include the status code and an optional response body.

StatusCodeResult and HttpClientResult

The current extension methods return either a Microsoft.AspNetCore.Mvc.StatusCodeResult or an EDennis.AspNetCore.Base.Web.HttpClientResult. The StatusCodeResult type is the regular Microsoft StatusCodeResult that can be cast to an int. The HttpClientResult type is a typed analog of Microsoft's ObjectResult, which includes both the response body and the status code. The HttpClientResult type has some properties and methods that make it easy to extract the desired data or to translate to an ObjectResult or StatusCodeResult.

HttpClientResult

Type Parameters Properties

HttpClientResult has one type parameter:

Type Parameter Constraints
T T is any primitive or class type, including a generic collection type

Properties

Property Description
public int StatusCode { get; set; } This is the HTTP response status code
public T Value { get; set; } This is the deserialized response body

Methods

Method Description
public ObjectResult ObjectResult() Transforms the result into a Microsoft.AspNetCore.Mvc.ObjectResult
public T Value { get; set; } Transforms the result into Microsoft.AspNetCore.Mvc.StatusCodeResult

HttpClient Extension Methods (From This Library)

What follows are descriptions and sample code for each of the extension methods. Below each example is the equivalent code required to produce the desired response-body/status-code return object.

Get

This library provides a generic Get method, which bundles up the response body and status code into an HttpClientResult.

Example

public HttpClientResult<IEnumerable<Color>> GetAll() {
    return HttpClient.Get<IEnumerable<Color>>(CONTROLLER_PATH);
}

Equivalent Example Using Microsoft Methods

public HttpClientResult<IEnumerable<Color>> GetAll_Microsoft() {
    var result = HttpClient.GetAsync(CONTROLLER_PATH).Result;
    var content = result.Content.ReadAsAsync<IEnumerable<Color>>().Result;
    return new HttpClientResult<IEnumerable<Color>> {
        StatusCode = (int)result.StatusCode,
        Value = content
    };
}

Post

This library provides a generic Post method, which bundles up the response body and status code into an HttpClientResult. Importantly, this response type gives the endpoints the option of returning a status code, a response body, or a response body and status code.

Example

public HttpClientResult<Color> Post(Color color) {
    return HttpClient.Post(CONTROLLER_PATH, color);
}

Equivalent Example Using Microsoft Methods

//requires Microsoft.AspNet.WebApi.Client NuGet package
public HttpClientResult<Color> Post_Microsoft(Color color) {
    var result = HttpClient.PostAsJsonAsync(CONTROLLER_PATH, color).Result;
    var content = result.Content.ReadAsAsync<Color>().Result;
    return new HttpClientResult<Color> {
        StatusCode = (int)result.StatusCode,
        Value = content
    };
}

Put

This library provides a generic Put method, which bundles up the response body and status code into an HttpClientResult. Again, this response type gives the endpoints the option of returning a status code, a response body, or a response body and status code.

Example

public HttpClientResult<Color> Put(Color color, string name) {
    return HttpClient.Put($"{CONTROLLER_PATH}/{name}", color);
}

Equivalent Example Using Microsoft Methods

//requires Microsoft.AspNet.WebApi.Client NuGet package
public HttpClientResult<Color> Put_Microsoft(Color color, string name) {
    var result = HttpClient.PutAsJsonAsync($"{CONTROLLER_PATH}/{name}", color).Result;
    var content = result.Content.ReadAsAsync<Color>().Result;
    return new HttpClientResult<Color> {
        StatusCode = (int)result.StatusCode,
        Value = content
    };
}

Delete

This library provides a generic Delete method, which returns a simple StatusCodeResult. (For this particular method, the type parameter is only used to differentiate the method from the Microsoft method with the same signature.)

Example

public StatusCodeResult Delete(string name) {
    return HttpClient.Delete<Color>($"{CONTROLLER_PATH}/{name}");
}

Equivalent Example Using Microsoft Methods

public StatusCodeResult Delete_Microsoft(string name) {
    var result = HttpClient.DeleteAsync($"{CONTROLLER_PATH}/{name}").Result;
    var content = result.Content.ReadAsAsync<Color>().Result;
    return new StatusCodeResult((int)result.StatusCode);
}

Forward

Sometimes, a developer needs to forward a parent HTTP request to a child API. For example, the developer may be using an application development framework that generates views that are designed to communicate directly with controllers that are designed to work directly with Linq, Entity Framework, or some other object-relational mapping software. Unfortunately, if the developer wants to insert a microservices layer between the user interface and controllers, then the developer may have some significant coding work to stitch everything together.

The current library provides a typed Forward method that takes the parent HttpRequest as a parameter; copies the request method, request body, query parameters, and relevant headers (and cookies) to a HttpRequestMessage; sends the HttpRequestMessage to some designated endpoint, and returns an HttpClientResult object.

(to be continued)

Clone this wiki locally