Coder Social home page Coder Social logo

httpclientgoodies.net's Introduction

HttpClientGoodies.NET

Build status NuGet

A set of useful utilities for the .NET HttpClient.

Installation

Install-Package HttpClientGoodies

What's it for?

These small but useful utilities will make your HttpClient life worth living.

Fastest way to profit

Here's an example of sending JSON content and reading JSON content in the least amount of code possible, while also providing Basic Authentication.

var dataToSend = new Todo {
    Text = 'Install this package'
};

var createdTodo = await RequestBuilder.Post('http://api.todos.com/todos/{id}')
    .BasicAuthentication("username", "password")
    .AddUrlSegment("id", 123)
    .JsonContent(dataToSend)
    .SendAsync()
    .AsJson<Todo>();

Console.WriteLine(createdTodo.Text);

That's cool, what else can it do?

Quite a lot! All methods that build the request are chainable! Let's asume the following for each snippet:

var builder = new RequestBuilder();
  • Headers:

    builder.AddHeader("X-MyHeader", "cool value");
  • Query parameters

    builder.AddQuery("searchText", "how do i get my girlfriend to tape her fingers together like a dinosaur");
  • Base URI + Resource URI separately

    builder.BaseUri("http://todos.com")
           .ResourceUri("api/todos");
  • URL segments

    builder.BaseUri("http://todos.com")
           .ResourceUri("api/todos/{id}")
           .AddUrlSegment("id", 123);
  • Authentication

    builder.Authentication("Basic", "<some base64 here>");
    
    // Basic auth shortcut (will base64 for you!)
    builder.BasicAuthentication("username", "password");
  • Setting content

    builder.Content(new HttpStringContent("hehehe"));
    
    // Want to send JSON?
    builder.Content(new JsonContent(new Todo()));
    
    // Can do you one better!
    builder.JsonContent(new Todo());
  • HTTP method

    builder.Method(HttpMethod.Get);
  • Getting the HttpRequestMessage to send

    var message = builder.ToHttpRequestMessage();
    await someClient.SendAsync(message);
  • Sending the request without manually calling ToHttpRequestMessage

    var response = await builder.SendAsync();
    // if you have a client instance you can pass it in.
    var response = await builder.SendAsync(client);
  • Reading content as JSON

    var response = await builder.SendAsync();
    var todo = await response.Content.ReadAsJsonAsync<Todo>();
    
    // Even better...
    var todo = await builder.SendAsync().AsJson<Todo>();
    
    // Custom settings? No problemo!
    var settings = new JsonSerializerSettings();
    var todo = await response.Content.ReadAsJsonAsync<Todo>(settings);
    var todo = await builder.SendAsync().AsJson<Todo>(settings);
  • Shortcuts for methods

    The following static methods are available: Get, Post, Put, Patch, Delete, Head, Options, Trace.

    var todo = await RequestBuilder
      .Get('http://todos.com/api/todos/123')
      .SendAsync()
      .AsJson<Todo>();

Author

Jeff Hansen - @Jeffijoe

httpclientgoodies.net's People

Contributors

jeffijoe avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.