Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC call REST Service from server side [closed]


During an action in an API controller (Server side) I have to call a REST service (I have an external REST service that return the user country by IP), wait for the returning results and continue execution...
What is the best way of doing so?

like image 310
Yaron Avatar asked Jan 24 '14 10:01

Yaron


1 Answers

You can find a tutorial using HttpClient here

Example for a Resource GET:

    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri("http://localhost:9000/");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        // New code:
        HttpResponseMessage response = await client.GetAsync("api/products/1");
        if (response.IsSuccessStatusCode)
        {
            Product product = await response.Content.ReadAsAsync<Product>();
            Console.WriteLine("{0}\t${1}\t{2}", product.Name, product.Price, product.Category);
        }
    }
like image 96
Jeroen K Avatar answered Sep 28 '22 03:09

Jeroen K