EasyHttp

As of late, much of the code I write, somehow or other has to communicate with an HTTP server. Be it a “ReSTful” service or a “Wanna-be-ReSTful” service, I’ve had the need to make GET, POST, PUT et al operations and work with JSON.

After writing smaller wrappers around WebRequest on a few occasions, I decided it’s time to formalize the wrapper. This has given way to EasyHttp. It’s been sitting on GitHub for quite some time and both myself and others have been using it for several projects, so I think it’s reached a point where more people can try it out if they wish.

Best way to describe the features of EasyHttp is with some code

 

var http = new HttpClient
           {
               Request = {Accept = HttpContentTypes.ApplicationJson}
           };

var response = http.Get("http://domain.com/customer/25");

var customer = response.StaticBody<Customer>();

 

The code is self-describing: we create an instance of HttpClient and indicate that we accept content-type application/json (since in this case the server sends us json). By specifying this Accept header, EasyHttp knows how to decode the request.

How do we obtain the response? In the code above we are using the StaticBody method which gives us back a strongly-typed Customer. But we have other options:

var http = new HttpClient
           {
               Request = {Accept = HttpContentTypes.ApplicationJson}
           };

var response = http.Get("http://domain.com/customer/25");

dynamic customer = response.DynamicBody();

Console.WriteLine(customer.Name);
Console.WriteLine(customer.Email);

 

In this case we want to return a dynamic type so we call the DynamicBody method. EasyHttp will automatically deserialize the response to a dynamic object. This allows us to access properties without having to declare types ahead of time (quite useful when working with JSON). Finally we can also get access to the raw response via the RawText property.

If we want to stream to a file, we simply do:

 

  var http = new HttpClient();

  http.GetAsFile("http://hadihariri.com/header.png", @"C:\Temp\header.png");

 

Working with other verbs is pretty much the same process. Let’s say we want to create a customer calling a service:

var http = new HttpClient();

var customer = new Customer()
                    {
                        Name = "Joe Smith",
                        Email = "Joe@Gmail.com"
                    };

http.Post("http://domain.com/customer", customer, HttpContentTypes.ApplicationJson);

 

In this case we are posting a Customer object and asking EasyHttp to encode it using application/json. Similar to when receiving a response, when making a request that requires a body, we can also use dynamic objects. As such, this would also work:

   var http = new HttpClient();

   dynamic customer = new ExpandoObject();

   customer.Name = "Joe Smith";
   customer.Email = "Joe@Gmail.com";

   http.Post("http://domain.com/customer", customer, HttpContentTypes.ApplicationJson);

 

Internally, EasyHttp is using the excellent JsonFX which currently provides support for JSON and XML encoding/decoding. EasyHttp extends this by adding encoding support for www-form-urlencoded. Extending it with other format should be pretty easy.

As well as GET and POST, EasyHttp also provides support for PUT, DELETE and HEAD. Request and Response headers are surfaced as properties, so instead of having to add headers manually, we can just assign them individually (some of these are surfaced from the existing WebRequest).

Summary

That’s pretty much all there is to it. As I mentioned, I’ve been using it myself to talk to CouchDB, as part of YouTrackSharp and a few other projects. It’s far from feature complete, but I’m adding things as I or the few that are using it request them.

Feel free to download it and play with it. The source code is on GitHub and Issue Tracker is on CodeBetter. If you are interested in running the tests, you will need CouchDB. I originally wrote this because I needed to do some things for CouchDB and it served as a good platform for testing different Http Verbs (yes, they are integrations tests and not unit tests).


Posted 01-16-2011 6:03 PM by Hadi Hariri
Filed under: , ,

[Advertisement]

Comments

Paul Batum wrote re: EasyHttp
on 01-16-2011 2:44 PM

Have you looked at Microsoft.Net.Http.dll ? Currently it is being released out of band as part of the HTTP work that Glenn Block is doing on WCF. There is a brief mention of it here in the HTTP Client section:

codebetter.com/.../wcf-web-apis-http-your-way

You can download it here:

wcf.codeplex.com/wikipage

Hadi Hariri wrote re: EasyHttp
on 01-17-2011 12:28 AM

@Paul

Yes I saw it recently although I've not played with it. I had this code sitting on my machine for months and projects using it, so no real necessity to switch or discontinue it.

DaRage wrote re: EasyHttp
on 01-17-2011 11:31 AM

Seems like a very nice library with a well designed API.

I have a question:  can the user control the serialization of the xml?

Hadi Hariri wrote re: EasyHttp
on 01-17-2011 12:25 PM

@DaRage,

Thanks. You can do pretty much anything with it since. By default, it allows you to use all sort of conventions and attributes (such as DatAcontract etc) to customize XML. More than that, you'd need to plug-in your own serializer which is also possible. What would you need to change?

Oded wrote re: EasyHttp
on 01-17-2011 12:40 PM

Is there a way to configure proxy settings?

DaRage wrote re: EasyHttp
on 01-17-2011 2:11 PM

@Hadi

Nothing really. I just wanted to know if the xml can be controlled. Thanks for the response.

SKOROZSI.NET wrote links for 2011-01-17
on 01-17-2011 3:01 PM

links for 2011-01-17

Hadi Hariri wrote re: EasyHttp
on 01-17-2011 4:19 PM

@Oded,

Not currently, but I've filed a request for you to add it.

youtrack.codebetter.com/.../EHTTP-18

David Fowler wrote re: EasyHttp
on 01-18-2011 5:21 AM

Thought about making this a nuget package? www.nuget.org

Hadi Hariri wrote re: EasyHttp
on 01-18-2011 6:22 AM

@David

Yes.  I need to. However it uses JSONFX and that needs to be done first. But it's on the list

gOODiDEA.NET wrote Cheatsheet: 2011 01.11 ~ 01.18
on 01-18-2011 6:52 AM

Database MongoDB, Data Durability and Improvements coming in 1.8 Riak's Bitcask - A Log-Structured Hash

hd movie wrote re: EasyHttp
on 01-23-2011 11:21 PM

<a href="filmspack.com/">hd movie</a>

shanzaymalik wrote re: EasyHttp
on 01-30-2011 10:15 AM

thanks for sharing this http post, i ma going to bookmark this

download megaupload wrote re: EasyHttp
on 03-21-2011 7:32 AM

<a href="rapidshare-zip.org/">download megaupload</a>

Andrew Huey wrote re: EasyHttp
on 05-19-2011 2:41 PM

Hey, this is a great thing.  I think it's really going to help me on my current project.  On typo though, in your second example:

dynamic customer = response.DynamicBody();

should be:

dynamic customer = response.DynamicBody;

on 01-18-2013 11:13 PM

VBHQdK Appreciate you sharing, great article.Really thank you! Fantastic.

generic stendra discount wrote re: EasyHttp
on 01-26-2013 9:47 PM

QQpXJ1 Major thanks for the blog post.Much thanks again. Really Cool.

lose weight pills wrote re: EasyHttp
on 02-02-2013 2:05 AM

OUi9YS wow, awesome blog article. Really Cool.

buy discount viagra wrote re: EasyHttp
on 02-03-2013 6:44 AM

A1C0OP Hey, thanks for the article.Really looking forward to read more. Much obliged.

http://bestmedicineonline.info wrote re: EasyHttp
on 02-15-2013 10:43 AM

eT51BK I really enjoy the article post.Thanks Again. Cool.

buy clomid no prescription wrote re: EasyHttp
on 02-24-2013 11:48 PM

jlsFSG Say, you got a nice post. Really Cool.

on 02-28-2013 10:17 AM

UB21Yc Awesome blog.Much thanks again. Want more.

click here wrote re: EasyHttp
on 03-02-2013 12:00 PM

kqG9N0 Appreciate you sharing, great blog.Really looking forward to read more. Cool.

buy viagra wrote re: EasyHttp
on 03-02-2013 12:16 PM

YmxAmg Major thankies for the article. Want more.

bookmaring service wrote re: EasyHttp
on 03-13-2013 3:44 PM

96iJHL Thanks a lot for the blog post. Want more.

social bookmarking service wrote re: EasyHttp
on 03-24-2013 11:49 AM

tsqrl7 Great, thanks for sharing this post.Thanks Again. Want more.

buy social bookmarks wrote re: EasyHttp
on 03-24-2013 5:31 PM

URwj32 Say, you got a nice blog article.Thanks Again. Fantastic.

buy social bookmarks wrote re: EasyHttp
on 04-03-2013 8:23 AM

jdl9xO A big thank you for your blog article.Really looking forward to read more. Really Great.

bears wrote re: EasyHttp
on 04-05-2013 3:09 PM

I truly appreciate this blog article.Much thanks again.

Social bookmarks wrote re: EasyHttp
on 04-07-2013 4:57 PM

8PBzjS Very good post. Great.

buy social bookmarks wrote re: EasyHttp
on 04-13-2013 10:09 AM

8ef9nB Im obliged for the blog article.Much thanks again. Great.

social bookmarking service wrote re: EasyHttp
on 04-23-2013 7:29 PM

Z1yotg Thanks-a-mundo for the blog post.Much thanks again.

digital slr lenses wrote re: EasyHttp
on 05-14-2013 3:35 AM

BScn5m Im thankful for the article. Much obliged.

seo pack wrote re: EasyHttp
on 05-19-2013 10:15 PM

MBEdrH Fantastic article post.Really thank you! Really Great.

Daniel wrote re: EasyHttp
on 05-29-2013 4:14 AM

Hi! I have used EasyHttp before and i liked it. But now I have encounted a small problem. I need to set authentication in the http header of a GET request. I want to set X-Accept: userKey. How is this done? Ive tried

http.Request.AddExtraHeader("X-Accept", UserKey);

but it doesn´t seem to work, or am i doing it all wrong?=)

green modular homes wrote re: EasyHttp
on 06-05-2013 3:29 AM

RtrXny Thanks so much for the post.Really thank you! Cool.

best social bookmarks wrote re: EasyHttp
on 06-19-2013 12:27 PM

2fTd5y Really informative article post.Really thank you! Will read on...

social bookmarks wrote re: EasyHttp
on 06-21-2013 6:31 PM

vkKCKT Looking forward to reading more. Great blog article. Fantastic.

cool news wrote re: EasyHttp
on 07-08-2013 10:17 AM

NoAVqI This is one awesome article post. Really Great.

news wrote re: EasyHttp
on 07-12-2013 11:58 PM

GOChgM Muchos Gracias for your article.Really thank you! Much obliged.

here wrote re: EasyHttp
on 07-23-2013 2:39 PM

Thanks-a-mundo for the blog.Thanks Again. Much obliged.

this site wrote re: EasyHttp
on 07-24-2013 4:20 PM

Say, you got a nice article post.Really looking forward to read more. Cool.

buy viagra online cheap wrote re: EasyHttp
on 07-25-2013 7:33 PM

Hey, thanks for the article post.Really thank you! Much obliged.

news wrote re: EasyHttp
on 07-26-2013 4:05 AM

O63ndk Really informative blog article.Really looking forward to read more. Fantastic.

social bookmarks wrote re: EasyHttp
on 07-29-2013 8:52 AM

Uutw5D Looking forward to reading more. Great article post.

best news wrote re: EasyHttp
on 08-02-2013 10:56 AM

hLe9jd Wow, great article post. Cool.

news news news news wrote re: EasyHttp
on 08-04-2013 11:49 PM

owf6Ot Awesome blog post.Much thanks again.

awesome links for you wrote re: EasyHttp
on 08-19-2013 10:44 AM

hqu3uB Very informative blog post.Much thanks again. Cool.

awesome links for you wrote re: EasyHttp
on 08-19-2013 10:19 PM

hNxx6X Awesome post.Really looking forward to read more.

great link buildng wrote re: EasyHttp
on 08-22-2013 2:59 AM

cSAPTZ I value the article post.Really looking forward to read more. Much obliged.

buy viagra online cheap wrote re: EasyHttp
on 09-02-2013 4:07 PM

Major thankies for the blog article.Much thanks again. Awesome.

great seo service wrote re: EasyHttp
on 09-03-2013 10:26 PM

gnnhA1 Major thankies for the post.Much thanks again. Really Cool.

the best seo service wrote re: EasyHttp
on 09-05-2013 8:03 PM

AmLxEn Really appreciate you sharing this article.

source wrote re: EasyHttp
on 09-08-2013 6:47 AM

Fantastic blog.Really looking forward to read more. Fantastic.

on 09-11-2013 6:01 PM

9GJiBX Looking forward to reading more. Great article.Thanks Again.

only for 5 dollars wrote re: EasyHttp
on 09-13-2013 10:41 AM

tMz0TG Appreciate you sharing, great post.Thanks Again. Really Cool.

awesome link building wrote re: EasyHttp
on 09-24-2013 2:01 PM

CBaEhD Im grateful for the blog. Great.

awesome linkbuilding site wrote re: EasyHttp
on 10-01-2013 1:42 AM

5NoOSl A big thank you for your blog article.Much thanks again. Fantastic.

link building team wrote re: EasyHttp
on 10-15-2013 12:06 PM

h4BG9l Looking forward to reading more. Great post.Thanks Again. Much obliged.

top seo guys wrote re: EasyHttp
on 10-26-2013 3:19 PM

CHutkl Im obliged for the post.Really looking forward to read more. Want more.

take a look at it! wrote re: EasyHttp
on 11-01-2013 4:35 PM

FGY9Vp Very good post.Much thanks again. Keep writing.

high quality backlinks wrote re: EasyHttp
on 07-19-2014 12:53 AM

hy3sT0 I appreciate you sharing this blog.Really looking forward to read more. Keep writing.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)