Skip to content

Extensions and utilities for System.Net.Http.HttpClient on .NET and .NET Core.

License

Notifications You must be signed in to change notification settings

ashimoon/dotnetliberty-http

Repository files navigation

Description

Extensions and utilities for System.Net.Http.HttpClient on .NET and .NET Core.

Contents

  1. Typed extensions for HttpClient
  2. Synchronous extensions for HttpClient
  3. JsonContent<TEntity> for easy JSON serialization

1. Typed extensions for HttpClient

GET/POST/PUT/DELETE of standard .NET objects using DataContractJsonSerializer. (Optional: Supports data contract annotations.)

Setup

Declare a model:

[DataContract]
public class Thing
{
    [DataMember]
    public int Id { get; set; }

    [DataMember]
    public string SomeProperty { get; set; }
}

Import extensions:

using DotNetLiberty.Http;

Create client:

var client = new HttpClient()
    .WithBaseUri(new Uri("http://localhost:5000/api/things/"))
    .AcceptJson();

GET /api/things/

IEnumerable<Thing> things = await client.GetManyAsync<Thing>();

GET /api/things/1

Thing thing = await client.GetSingleAsync<Thing>(1);

POST /api/things/

Thing created = await client.PostAsync(new Thing {
    SomeProperty = "Some value"
});

PUT /api/things/1

Thing updated = await client.PutAsync(1, new Thing {
    Id = 1,
    SomeProperty = "Some value"
});

DELETE /api/things/1

await client.DeleteAsync(1);

2. Synchronous extensions for HttpClient

System.Net.Http.HttpClient only supports asynchronous variants of operations out of the box. Rather than handle Waiting and unpacking exceptions we can use synchronous extension methods.

Setup

Import extensions:

using DotNetLiberty.Http.Sync;

Create client:

var client = new HttpClient()
    .WithBaseUri(new Uri("http://localhost:5000/"));

Samples

HttpResponseMessage response = client.Get("");
HttpResponseMessage response = client.Post("", new StringContent(""));
HttpResponseMessage response = client.Put("", new StringContent(""));
HttpResponseMessage response = client.Delete("");
var request = new HttpRequestMessage();
HttpResponseMessage response = client.Send(request);

3. JsonContent<TEntity> for easy JSON serialization

Rather than having to serialize and encode your entities into a StringContent instance manually, you can use a JsonContent<TEntity>(TEntity) instead.

Sample

Thing thing = new Thing {
    Id = 1,
    SomeProperty = "Some value"
};
StringContent content = new JsonContent<Thing>(thing); 
HttpResponseMessage response = await client.PostAsync("", content);

Website

.NET Liberty - http://dotnetliberty.com

About

Extensions and utilities for System.Net.Http.HttpClient on .NET and .NET Core.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages