Skip to content
Johan Leino edited this page May 19, 2013 · 6 revisions

According to W3 the Get method does the following.

The GET method means retrieve whatever information (in the form of an entity) is identified by the Request-URI. If the Request-URI refers to a data-producing process, it is the produced data which shall be returned as the entity in the response and not the source text of the process, unless that text happens to be the output of the process.

There are several ways you can do a Get with Easyhttp. In all the below examples we assume there is a service on the other side which returns objects of the type tree with an Id property and a Genus property.

The url /trees?Id=1 will give us a single tree object which we can get like this.

var http = new HttpClient();
http.Request.Accept = HttpContentTypes.ApplicationJson;
var result = http.Get("http://localhost/trees?Id=1");
var tree = result.DynamicBody;
Console.WriteLine(tree.Id);
Console.WriteLine(tree.Genus);

Or we can use an anonymous object like this.

var http = new HttpClient();
http.Request.Accept = HttpContentTypes.ApplicationJson;
var result = http.Get("http://localhost/trees", new {Id = 1} );
var tree = result.DynamicBody;
Console.WriteLine(tree.Id);
Console.WriteLine(tree.Genus);

The anonymous object will translate in adding ?Id=1 to the url and then doing the get.

We can also choose to use a dynamic ExpandoObject like this (which will result in the same result as the anonymous object)

var http = new HttpClient();
http.Request.Accept = HttpContentTypes.ApplicationJson;
dynamic parameters = new ExpandoObject();
parameters.Id = 1;
var result = http.Get("http://localhost/trees", parameters );
var tree = result.DynamicBody;
Console.WriteLine(tree.Id);
Console.WriteLine(tree.Genus);

Our service might not be able to interpret the named parameters url and only understand segments like this /trees/1.

Then we can instruct the easyhttp request object to make our url like that while still using an anonymous object. Like this.

var http = new HttpClient();
http.Request.Accept = HttpContentTypes.ApplicationJson;
http.Request.ParametersAsSegments = true;
var result = http.Get("http://localhost/trees", new {Id = 1});
var tree = result.DynamicBody;
Console.WriteLine(tree.Id);
Console.WriteLine(tree.Genus);

The above would be the same as this

var http = new HttpClient();
http.Request.Accept = HttpContentTypes.ApplicationJson;
var result = http.Get("http://localhost/trees/1");
var tree = result.DynamicBody;
Console.WriteLine(tree.Id);
Console.WriteLine(tree.Genus);

Be aware that the order of the properties in the anonymous object is important.

For example

http.Get("http://localhost/trees", new {Id = 1, Name = "test"});

will result in this url http://localhost/trees/1/test

while the following code

http.Get("http://localhost/trees", new {Name = "test", Id = 1});

Will result in.

http://localhost/trees/test/1

Instead of using Anonymous objects we can of course also use pocos (ExpandoObjects are also supported) as parameters.

If your service responds with a list of objects than you can read them like this.

var http = new HttpClient();
http.Request.Accept = HttpContentTypes.ApplicationJson;
var trees = http.Get("http://localhost:55360/trees");
foreach(var t in trees.DynamicBody)
{
   Console.WriteLine(t.Id);
   Console.WriteLine(t.Genus);
}

You can also stream the response.

Or you can get a file.

Clone this wiki locally