Tony
Tony

Reputation: 21

How to call node.js REST API from .NET

If I put the jquery code below within the script tag within a html page and drag the html page into a web browser the call to the API specified in the URL is made and I get back a response in JSON format. So this works good.

The reason I want to use .NET for calling the rest API that is made in node.js is because I want to use the unit test utility that exist in visual studio.

So when I start the unit test the call to the REST API made in node.js should be made and then I can check whatever I want in the returned json format by using the assert.AreEqual.

I have googled a lot and there is several example about Unit Testing Controllers in ASP.NET Web API 2 but I don't want to unit test controller. I only want to call the REST API(made in node.js) when I start my unit test.

I assume to use .NET in the way I want is probably quite rare.

If it's not possible to use .NET and unit test in the way that I want here I will use another test framework. I hope to get some help from here.

Hope you understand what I mean.

$.ajax({
    type: 'GET',
    url: 'http://10.1.23.168:3000/api/v1/users/1',
    dataType: 'json',
    async: false,
    headers: {
    'Authorization': 'Basic ' + btoa('DEFAULT/user:password')
    },
    success: function(response) {
        //your success code
        console.log(response);
    },
    error: function (err) {
        //your error code
        console.log(err);   
    }   
});

Many thanks

Upvotes: 1

Views: 7498

Answers (2)

Urasquirrel
Urasquirrel

Reputation: 1585

Here's the example source with all the rest of the code.

Another resource is the docs.

This code snippet should be enough to get you where you need.

using(var client = newHttpClient())  
{  
    client.BaseAddress = newUri("http://localhost:55587/");  
    client.DefaultRequestHeaders.Accept.Clear();  
    client.DefaultRequestHeaders.Accept.Add(newMediaTypeWithQualityHeaderValue("application/json"));  
    //GET Method  
    HttpResponseMessage response = awaitclient.GetAsync("api/Department/1");  
    if (response.IsSuccessStatusCode)  
    {  
        Departmentdepartment = awaitresponse.Content.ReadAsAsync < Department > ();  
        Console.WriteLine("Id:{0}\tName:{1}", department.DepartmentId, department.DepartmentName);  
        Console.WriteLine("No of Employee in Department: {0}", department.Employees.Count);  
    }  
    else  
    {  
        Console.WriteLine("Internal server Error");  
    }  
}

Upvotes: 0

Azargoth
Azargoth

Reputation: 395

Basically what you need to do is to call node.js' API from your C# test code in a same way you call it using jQuery. There are several ways to do it:

  1. Use HttpWebRequest class https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest%28v=vs.110%29.aspx
  2. Use HttpClient class https://msdn.microsoft.com/en-us/library/system.net.http.httpclient%28v=vs.118%29.aspx It's more "RESTable" since it exposes methods to call HTTP methods like GET, PUT, POST and DELETE methods directly.
  3. 3rd party software http://restsharp.org/

Generally I recommend approach #2.

Upvotes: 2

Related Questions