Hey I'm looking for some tutorials on how to consume an external Web API in ASP.NET MVC or if someone could explain briefly on how to go about it,specifically the https://platform.fatsecret.com/api/
You should check out RestSharp . It makes it very easy to consume an external API in your .NET application, since you can control how the API-response (JSON or XML) is deserialized to your model classes.
You can refer to the websites REST API Documentation on everything you need to include in your requests to their server. Here is an example of a method https://platform.fatsecret.com/api/Default.aspx?screen=rapiref&method=food.get
It breaks it down into what is required to be in your request and what you are to expect to see returned.
As for actual code, you have a few options. You can refer to Microsoft Docs in order to learn how to actually craft, send and receive requests to a REST API. The example shown uses the asp.net client Nuget package. It provides an object which allows you to easily create and receive requests.
Personaly, I like to practice with Postman for Chrome first. It allows you to easily create and receive REST data and even has an option to create template code from your request into multiple languages!
Related
We have a Web API project now I have to create a tool where I can test the methods of the Web API. I need to create a tool where if I give the request I need to get the XML response in UI. What approach should I follow?
You can use SwaggerUI as documentation and request testing tool. More information about Swagger can be found at their official site.
There are some helpful guides how to configure SwaggerUI for your application:
1. ASP.NET MVC Web API 2: https://dotnettutorials.net/lesson/how-to-use-swagger-in-web-api
2. ASP.NET Core Web API: https://learn.microsoft.com/en-us/aspnet/core/tutorials/getting-started-with-swashbuckle
Use Accept header while sending the request as "application/json" or "application/xml". Add xml formatting to list of formatters if not already done.
Read more on content negotiation here
https://learn.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/content-negotiation
I am not sure what you mean by saying that you need to get the XML response in UI. If you need to test something manually and actually see the returned response, then Swagger is the most popular and easy-to-use option I would suggest you.
However, if you need to write automated tests, I would highly recommend you to try the MyTested ASP.Net framework. I personally use it in every web project that I do. It is pretty easy to start writing all kind of tests, it has a lot of functionalities within and can be configured for almost every case. The author is a very kind person and a friend of mine that dedicates most of its time on that project and can always relate to your problems (if such exist) and needs to build a better product.
If you are looking to perform integration tests against your APIs, you can use Postman: Postman
I am learning how to consume a webhook in ASP.Net MVC solution, the webhook can be from OneDrive, GitHub or Twilio (or anyone), but I am not able to find even a basic example of consuming webhooks. I tried different keyword and searched the web but I haven't found a basic tutorial for this.
I am willing to put the controller code of my MVC Project but I don't know how to consume webhooks.
I did my research and I could not find any basic example for consuming webhooks. I am starting to wonder is there any synonym terms for webhooks I should be entering into a search engine?
Links I looked into: VS Hooks
To listen a webhook you basically need a controller with some method that allows HttpPost request. The system who fires the webhook must have some doc/config to know method name and params. Usually you need a DNS or public ip to get the request
#Mate already answered this using example from https://ngrok.com/
I want to build a rest api for a project, So currently i am trying to build something using twitter api.
I have registered a new application on twitter. I have got following keys after registeration
Consumer key
Consumer secret
Request token URL
Access token URL
Authorize URL
Registered OAuth Callback URL
So what are these keys and where does all these fits in a Rest api design considerations
Thanks :)
These are OAuth parameters and the best place to put them is in the Authentication header. See the example in section 5.4.1. here http://oauth.net/core/1.0/#consumer_req_param.
Authentication is an issue completely independent of REST and therefore to answer your question, those parameters you listed have absolutely nothing to do with RESTful design.
If you are interested in patterning your restful API after Twitter's, then I suggest studying the way Twitter has organized their API resources (see the method docs under the links in the right sidebar here), how they support different resource formats (by changing the extension, like .xml or .json, of the requested resource), how they version the endpoint, and which verbs are supported per resource (for example, how the oauth/request_token method supports both POST and GET methods).
So, study the interface that Twitter produced, ignoring the actual implementation behind it for now, and use it to help answer your design questions. Focus on what you want to expose through your interface. How do you plan to project the objects you expose via the interface? What resource formats will you choose to support? How do you plan to version the service endpoint?
By the way, if you're interested in looking at other restful APIs, you can spelunk around using apigee; they have a console for Twitter here.
I am creating a few basic web services using c#, and I am trying to have the web service return back just a normal name=value&name=value without any kind of xml or json format. The legacy system hitting these services is fairly old and doesn't support xml or json. Is doing this possible?
If the legacy service that's targeting this web service is that old, how exactly are you calling the web service from it? It may be easier to create an .aspx page (or even better, .ashx) that parses the request and makes the response simply using Response.Write.
If you update your question/add a comment with the detail about how you're calling the service, I'll update my answer accordingly =)
I am thinking to start writing some REST web services as a way to provide data. I guess that when my REST web services are available, then some of my web applications and console applications will be able to use REST web service as data service to get, add, update and delete data to databases. In addition to that, I would like to add authentication feature to identify any request.
My question is that where should I start? I saw Microsoft ADO.Net Data Services. Not sure if this is a good start place? Are there any examples available?
Check out the REST in WCF MSDN site and the starter kit. Good article here too.
You may also want to check out servicestack.net An Open Source, cross-platform, high-performance web service framework that lets you develop web services using code-first, strongly-typed DTO's which will automatically (without any configuration) be immediately available on a variety of different endpoints out-of-the-box (i.e. XML, JSON, JSV, SOAP 1.1/1.2).
REST, RPC and SOAP out of the box
In addition, your same web services can also be made available via any ReST-ful url of your choice where the preferred serialization format can be specified by your REST client i.e.
Using the HTTP Accept: header
Appending the preferred format to the query string e.g. ?format=xml
See the Nothing but REST! web service example for how to develop a complete REST-ful Ajax CRUD app with only 1 page of jQuery and 1 page of C#.
A good place to start is the Hello World example to see how to easily add ServiceStack web services to any existing ASP.NET web application.
Performance
For the performance conscience, ServiceStack makes an excellent Ajax server as it comes bundled with the fastest JSON Serializer for .NET (> 3x faster than other JSON Serializers).
Checkout this live Ajax app for a taste (Live demo hosted on Linux/Nginx/MONO).
Simple Northwind Example
ServiceStack also makes it easy to create strong-typed frictionless web services where with just the code below is all you need to return a List of Customer POCOs:
public class CustomersService : RestServiceBase<Customers>
{
public IDbConnectionFactory DbFactory { get; set; }
public override object OnGet(Customers request)
{
return new CustomersResponse { Customers = DbFactory.Exec(dbCmd =>
dbCmd.Select<Customer>())
};
}
With no other config, you can now call the above webservice REST-fully returning all of:
XML
JSON
CSV
HTML
JSV
SOAP
Accessing web services on the client
You can call the above web service re-using the same DTOs that your web services were defined with (i.e. no code-gen is required) using your preferred generic ServiceClient (i.e Json, Xml, etc). This allows you to call your web services using a strong-typed API with just 1 Line of code:
C# Sync Example
IServiceClient client = new JsonServiceClient("http://host/service");
var customers = client.Send<CustomersResponse>(new Customers());
And since your web services are also REST services, it works seamlessly with JavaScript ajax clients, e.g:
Using jQuery
$.getJSON("http://host/service", function(r) { alert(r.Customers.length); });
ASP.NET Web API is now the Microsoft framework for creating RESTful services.
http://www.asp.net/web-api
If you are new to REST in the .net world then start with OpenRasta. The other Microsoft solutions can do REST if you work hard at it but they will guide you down a route where you will most likely end up with POD(Plain old data) over HTTP. That is not what REST is all about. If that's all you want then that's cool too, but it is not REST.
If you're going WCF, the WCF REST Starter Kit that JP referred to is a great place to start.
Omar Al Zabir provides a pretty good example of using ASP.NET MVC to provide RESTful services that are fluent in both XML and JSON
You can also go the ADO.NET Data Services route you suggested. These services are built on top of the WCF stack.
I've never stumbled across any really good guidance on how to select between these options. In ASP.NET MVC you take on the majority of the plumbing burden but also have maximal control. Straight RESTful WCF is the happy middle ground although WCF tends to want to have things done its way. ADO.NET Data Services are pretty magical with the downside of buying fully into a given approach to generating these services and losing more flexibility.
There are a couple of good books you can read on the topic of RESTful services with .NET. Both O'Reilly and Microsoft Press have recently released books on this topic. Perhaps the most important advice I can provide you is to consume and understand several open RESTful services (e.g. Twitter, Amazon, Flickr) to understand the design decisions that went into creating the services. User provisioning, authentication mechanism, and supported content types (e.g. JSON, XML, RSS/ATOM) are some of the decisions that you can observe in action to aid you in your path to creating your service API.