• Stars
    star
    62
  • Rank 473,926 (Top 10 %)
  • Language
    C#
  • License
    MIT License
  • Created over 7 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Useful utilities for the .NET HttpClient.

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

More Repositories

1

awilix

Extremely powerful Inversion of Control (IoC) container for Node.JS
TypeScript
2,999
star
2

typesync

Install missing TypeScript typings for dependencies in your package.json.
TypeScript
1,390
star
3

koa-es7-boilerplate

A boilerplate for writing Koa 2 apps in ES7 with Babel.
JavaScript
274
star
4

mobx-task

Makes async function state management in MobX fun.
TypeScript
237
star
5

messageformat.net

ICU MessageFormat implementation for .NET.
C#
148
star
6

awilix-koa

Awilix helpers/middleware for Koa 2
TypeScript
129
star
7

yenv

Environment management for Node using YAML.
JavaScript
104
star
8

koa-respond

Koa middleware that adds useful methods to the context.
JavaScript
102
star
9

awilix-express

Awilix helpers/middleware for Express
TypeScript
101
star
10

libx

Collection + Model infrastructure for MobX applications.
TypeScript
99
star
11

snicket

Stream Store based on Postgres for Node.JS
TypeScript
39
star
12

screengun

A simple screen recorder for Windows based on ffmpeg
C#
39
star
13

fejl

Error-making utility for Node apps.
TypeScript
35
star
14

logpipe-server

The Logpipe server.
JavaScript
30
star
15

awilix-router-core

Reusable building blocks for writing Awilix router adapters for HTTP frameworks.
TypeScript
25
star
16

validx

Validation library for MobX.
TypeScript
23
star
17

keyblade

Fail fast when accessing undefined properties on objects.
JavaScript
14
star
18

npm-module-boilerplate

A boilerplate for authoring npm modules, with tests and linting.
JavaScript
10
star
19

skadi

A simple object validator/sanitizer based on `is-my-json-valid`.
JavaScript
9
star
20

deltio

A Google Cloud Pub/Sub emulator alternative, written in Rust.
Rust
8
star
21

dration

Duration utilities for JavaScript using primitive types.
TypeScript
6
star
22

icebug

A wrapper around node-inspector and nodemon.
JavaScript
4
star
23

jQuery-Validator.Knockout

KnockoutJS bindings for my jQuery-Validator.
JavaScript
4
star
24

bristol-sentry

Sentry integration for the Bristol logger
JavaScript
3
star
25

smid

Catches errors and returns them. Useful for unit testing.
TypeScript
3
star
26

baconpi-web

BaconPi Web Frontend + Backend
JavaScript
3
star
27

ts-module-boilerplate

TypeScript module boilerplate for Node packages
TypeScript
3
star
28

posish

A tool that makes counting string lines, columns and indexes awesome. Useful for writing parser tests.
JavaScript
2
star
29

total-rename

Utility to replace strings in files and paths.
Go
2
star
30

purge

Windows utility for deleting a directory tree with long paths.
C#
1
star
31

chilli

Rename files to sort-friendly numbers
F#
1
star
32

jQuery-Validator

A formless validation plugin for all kinds of validation - even your own!
JavaScript
1
star