Consuming REST services using Visual Studio 2010

Aetna is dipping its toe into public facing APIs via Carepass.  I signed up for a license and wanted to see how easy it was to consume some HHS drug data found here.  I first checked a GET inside a browser with the following uri:

https://api.carepass.com/hhs-directory-api/drugs/search?name=Cymbalta&apikey=xxx

and I got my results:

image

I then fired up Visual Studio to see how easy it would be to make a REST WCF call.  Turns out, there is not a way to add-> service reference like you can with SOAP.  This means I am turning back the clock of HttpWebRequest and HttpWebResponse.  Interestingly, there are not very good examples on Stackoverflow and MSDN about consuming non-WCF REST services.  The best example I found was here on Yahoo!

I created a simple ConsoleApp (changed it target runtime from client profile to full .NET 4.0) and launched into code:

static void Main(string[] args)
{
    Uri address = new Uri(@"https://api.carepass.com/hhs-directory-api/drugs/search?name=Cymbalta&apikey=t8bnhyvu9hj7eh4c8tg97jgb");   
    HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;   
    request.Method = "POST";   
    request.ContentType = "application/x-www-form-urlencoded";   

    using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)   
    {   
        StreamReader reader = new StreamReader(response.GetResponseStream());   
        Console.WriteLine(reader.ReadToEnd());   
    }

    Console.ReadKey();
}

Sure enough, it worked like a charm:

image

So then the next problem – how to I parse this blob of name/value pairs?  And is there hierarchical data in there?  I first thought (hoped) it would be easy – just split by the ‘:’

using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)   
{
    StreamReader reader = new StreamReader(response.GetResponseStream());
    String output = reader.ReadToEnd();
    Console.WriteLine("Response was: " + output.Length);
    String[] splitOutput = output.Split(':');
    foreach (String item in splitOutput)
    {
        Console.WriteLine(item);
    }
}

No dice:

image

I then switched my delimited to a comma:

image

That is actually better.  The next thing is seems is to check to see if the comma is inside the double quotes.  I know that the industry is going to REST from SOAP – but this is so much worse than adding a reference in Visual Studio….

3 Responses to Consuming REST services using Visual Studio 2010

  1. Rob Seder says:

    Jamie,

    You “should” be able to take that JSON and use the DataContractSerializer (see: http://msdn.microsoft.com/en-us/library/bb412170(v=VS.110).aspx) to deserialize that directly into objects.

    That means of course that you’d need to have data structures to hold that data. So, I don’t know if they give you that detail, but that might be the approach. If this were a SOAP web service, you’d already have the data structures -but with REST, I think you have to manually create them.

    Since this is the “new world order” of structureless data, now sure that would work here, but it might be worth a shot?

    -Rob

  2. Xymilato says:

    WebAPI REST client was purly made for “client” calls back in 2012. There was a clear distinction
    -> Consumer is a c# service use WCF
    -> Consumer is a form of javascript like platforms

    Now in 2014 you see the development that WebApi updates much faster then de WCF variant. EF6 support is in beta for WCF and finished for WebApi. WebApi supports OData v4 and WCF v3. etc.

    They also released a T4 template to generate a proxy for c# services.
    (http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/create-an-odata-v4-client-app)
    So now I think it’s Always better to go for the WebApi variant.

Leave a Reply to jamie dixon Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: