JsonHttp - A NuGet library for easy JSON API communication (+ source code)

Glenn Ruysschaert

New member
Jan 26, 2014
74
0
0
Hello, I found that a lot of the code necessary to communicate with a JSON API is always the same. So I compiled the code I frequently use into a NuGet package and released it.

It is the second version, so it might be buggy and doesnt have that many features yet. Plus it's my very first NuGet package I made, and only spent about 3 hours on it (so it's kind of a trial). I decided to release it already anyway, so it might help others code faster.

The usage is really simple, for example to get the word of the day on Urban Dictionary I have this class:
Code:
public class WordOfTheDay
{
    public string word { get; set; }
    public string meaning { get; set; }
}

Then all I need to do is use this line of code, and I will get the word of the day in the class I gave.
Code:
WordOfTheDay wotd = await JsonHttp.Get<WordOfTheDay>(new Uri("http://urban-word-of-the-day.herokuapp.com/today"));

That's it! No more messing with HttpClient, handlers, ... As for now it obviously only supports basic functionality, but I'm open to requests.

It's possible to add some extra options:
Code:
JsonHttp.Options options = new JsonHttp.Options()
{
    AllowAutoRedirect = true,
    DefaultRequestHeaders = new Dictionary<string, string>(),
    AddMediaTypeWithQualityHeadersJson = true,
    UseLocationHeaderForRedirects = true
};
WordOfTheDay wotd = await JsonHttp.Get<WordOfTheDay>(new Uri("http://urban-word-of-the-day.herokuapp.com/today"), options);

For POST/PUT you can also let a class be automatically converted to JSON and sent with your request
Code:
WordOfTheDay toPost = new WordOfTheDay()
 {
     word = "test",
     meaning = "something"
 };
 WordOfTheDay wotdPost = await JsonHttp.Post<WordOfTheDay>(new Uri(""), toPost, options);

You can try it out here (or look for JsonHttp).
Source code and documentation here on GitHub