Unirest for .NET (C#, VB.NET, etc) Desktop and Windows 8 apps
After the successful launch of Unirest, an open source lightweight http client library, we received a lot of requests for a C# .NET port on Windows Desktop and Windows 8. Even before we got to work, we’ve received contributions from the open source community who ported Unirest to .NET.
This tutorial will show you how to use the Windows 8 (RT) port of Unirest in your application (We will follow this up with the Windows Desktop port in a later post, but it will follow pretty much the same steps). Let’s get started!
NOTE: We will be updating this post when the Github repos are consolidated into http://unirest.io/
Windows 8 (RT)
- Download unirest-rt from https://github.com/michaelsync/unirest-rt . It will contain the project that we will add to our own Windows 8 project. Unizip to a folder of your choice.
- Fire up Visual Studio (Express) for Windows 8 and create a new blank project. Our app will simply display a response from the Yoda Speak API using unirest-rt. At this point you can drag a “TextBlock” into the MainPage.xml canvas where we will display this response. This is how your canvas should look like:
- Now let’s import the Unirest-rt library into our application. To do that, right click on your Solution (project) and click on Add -> Existing project. Navigate inside the Unirest-rt library you downloaded earlier and locate the unirest-rt.csproj to add it to your solution. You should locate it like this:
- After adding the Unirest-rt library project, your Solution Explorer will now look like this (Note that eventually this will be added as a Nuget package for easier library installation):
- Now we need to add this project as a reference to our main app project. To do that, right click on your app project and click “Add Reference”. Tick the unirest-rt checkbox and click Ok.
- The unirest-rt library requires the Microsoft.Bcl.Build package. You can pull that from Nuget by clicking on Tools -> Library Package Manager -> Manage Nuget packages. Search for “Microsoft.Bcl.Build” and select it, then Install.
- Now we can start coding! Open MainPage.xml.cs and add “using unirest_rt.http;” at the top to include the Unirest namespace. Then inside the OnNavigatedTo event, type in the code as below:
What this does is call the Yoda Speak API from Mashape through a Unirest HTTP get call, passing the same URL/parameter in Mashape’s test console. The response “Body” would then be displayed in the textblock that we added to the canvas in the earlier steps. (Note that the Mashape Authorization key here is blurred out because it is intended to be private and you’re supposed to use your own key. You can get it from your Mashape dashboard.)
- Once you’re done, hit F5 and your app should load and display the following:
We’ll follow up this post with instructions for Unirest for Windows Desktop. If you want to learn more about Unirest, please head over to http://unirest.io/
Let us know if you have questions! email@example.com