0

I want to see how can I get real data from a WebApi. All I see online is something like this :

 // GET api/<controller>
        public IEnumerable<Customer> Get()
        {
            CustomerContext customersdb = new CustomerContext();
            return customersdb.Customers;
        }

I actually want to make a GET request to facebook api for example and pull data from there. How do I write the GET method?

gregmac
  • 24,276
  • 10
  • 87
  • 118
Besa
  • 517
  • 1
  • 8
  • 19
  • http://facebooksdk.net/docs/web/getting-started/ , you don't need to make raw requests use the .Net library they provide. – Xi Sigma Mar 23 '15 at 20:02
  • I just took facebook as an example. I don't need anything complicated with access token . I just need to do a simple GET request to a real website for data. How should I do that – Besa Mar 23 '15 at 20:04
  • google for web api sandbox and you will get a lot of real web api's to test against. Google APIs would be a good place to start since they wont mind the load that you would put on them – Shashank Shekhar Mar 23 '15 at 20:05
  • try this https://developers.google.com/books/docs/v1/using – Shashank Shekhar Mar 23 '15 at 20:06
  • @Besa http://stackoverflow.com/questions/17415709/how-to-use-verb-get-with-webclient-request , simply using the `HttpWebRequest` class the is available in `System.Net` – Xi Sigma Mar 23 '15 at 20:07
  • The Web API here is what *you* provide. Fetching data from someone else's web API has nothing to do with this. :) – bzlm Mar 23 '15 at 20:12
  • Just add a reference to the external web service in your Web API project, query it and return the data in your GET method. – Francis Ducharme Mar 23 '15 at 20:30

1 Answers1

2

MS WebApi is used primarily to build REST services (the server side), and you seem to be asking about consuming them (the client side).


When you want to retrieve data from it, you are acting as an HTTP client -- and it does not matter that the server is built with WebApi or any other language/framework: in fact that is partly the point of doing things over HTTP (and REST).

All that matters from a client perspective is what is returned. These days, most APIs return JSON data, though some use XML or support multiple data types.

For a .NET web client, you can use System.Net.WebClient to get raw data, or one of many smart REST client libraries that let you immediately use retrieved results by deserializing them into a typed object.

For example, from an answer by @wonea using RestSharp:

var client = new RestClient("192.168.0.1");
var request = new RestRequest("api/item/", Method.GET);
var queryResult = client.Execute<List<Items>>(request).Data;
Community
  • 1
  • 1
gregmac
  • 24,276
  • 10
  • 87
  • 118