Back in days when I was working with Ruby on a MacBook Pro, I got Curl. However, now as I am using a PC in a .NET shop, I need to use compatible technology to test API – HttpClient is my solution.
Install the Web API Client Libraries via Package Manager Console
Install-Package Microsoft.AspNet.WebApi.Client
Sample code as below:
- [Fact] is merely an XUnit notation indicating that this is a unit test
- using creates an HttpClient instance which is only active within the scope
- await suspends operation until the execution is completed
- Method is declared asĀ async as HttpClient methods perform I/O
- ReadAsStringAsync() will read the output as string
- JsonConvert.Deserializeobject<> will parse the string into the object model LanguageDictionary
- You can use many methods from the HttpResponseMessage to verify the response from the API
Leave a Reply