4

I am trying to understand what exactly a REST-based API is. From what I understand it is just a convention for writing the functions within the API? All functions should be of either GET/POST/DELETE/PUT form? So, for example a function in a REST API could be

public string getLastName(User x)
{
    return x.lastName;
}

I am mainly confused about how JSON/XML play a role in this?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
SKLAK
  • 3,825
  • 9
  • 33
  • 57
  • `RESTful` services are best for `CRUD` operations and accessing resources through a single interface. It's a more simplified version of `SOAP`. `SOAP` is best at exposing operations and exposing application logic. – Brandon Jun 07 '14 at 00:44
  • REST is not CRUD, and attempts to conflate REST to CRUD often lead to poor design. REST is definitely not a more simplified version of SOAP. REST is probably the exact opposite of everything that defines SOAP. – Pedro Werneck Jun 07 '14 at 20:02

5 Answers5

3

Its more than just a convention. The concept behind a REST API is that you access it using the HTTP verbs, and that the functions those verbs have been mapped to perform the described action.

For example:

GET will return data to the caller/sender

DELETE will delete a record

And it goes further, but a lot of it is based on relying on HTTP to provide a level of consistency. For example, in a RESTful service, you might use the Accept HTTP header to request a JSON response or an XML response by supplying the application/json or application/xml values, respectively. This is just a simple example, and it is up to the implementer to decide how their API will work, but it highlights the importance placed on leveraging HTTP.

Why JSON/XML?

Along the same lines, JSON and XML are used because they are widespread and standard ways of representing and transmitting data over the web. JSON (JavaScript Object Notation) is very common in doing data transfer (especially on GET requests) due to most requests coming from JavaScript, and JS can easily interact with JSON without having to do the parsing required when dealing with XML. On the other hand, XML provides its own benefits, such as the ability to use schemas and namespaces. You may already be aware of benefits/drawbacks of each, but that's a separate discussion. The main point is that JSON/XML are the primary ways of communicating data in a REST API due to both of them being the de facto standards of the web.

There are lots of good resources for more information, this MSDN article may be helpful: http://msdn.microsoft.com/en-us/library/dd203052.aspx

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • @pep excellent points. If you wanted to add those in an edit (feel free to give yourself attribution!) I would be happy to accept it. – BradleyDotNET Jun 07 '14 at 01:14
  • @pep, I went ahead and integrated your edit. I didn't get it to it before the review queue (that's why it was initially rejected). Thanks again for your contribution! – BradleyDotNET Jun 07 '14 at 04:06
  • REST is not dependent on any communication protocol, so the idea that the concept behind REST is anything related to HTTP verbs doesn't make sense. This answer reflects many of the misconceptions about REST that are widespread on the web. – Pedro Werneck Jun 07 '14 at 19:23
  • @PedroWerneck, This answer reflects the practical realities of programming in REST, whether that matches the original theory is largely (IMHO) irrelevant. I read your answer, and have commented to that effect. Thanks for your feedback though! – BradleyDotNET Jun 08 '14 at 00:00
  • Your answer claims to be based on the concept behind REST, yet you say whether that matches the original theory is irrelevant? Sorry, that doesn't make much sense. You can't talk of the concept behind REST without going into the original theory. The only practical reality of REST that your answer reflects is how it became a buzzword to refer to any HTTP API that isn't SOAP. – Pedro Werneck Jun 08 '14 at 00:40
  • @PedroWerneck, Perhaps we should come up with a new name then :) – BradleyDotNET Jun 08 '14 at 00:48
  • Well... among the REST community there are some who believe this is another lost battle in the terminology wars and we should start using Hypermedia APIs to refer to real RESTful APIs. I prefer the opposite approach, to use a new name for the non-REST APIs. I usually call Street-REST the APIs that implement REST constraints arbitrarily, which are the most common, and Informed-REST the APIs that aren't RESTful but make informed decisions on which constraints to follow and which to waive. – Pedro Werneck Jun 08 '14 at 00:53
  • @PedroWerneck, I'm sure there will be a consensus some day... (then again, maybe not). Thanks for all your insight and information, I know I have learned something, and hopefully those that read our answers and comments will as well. – BradleyDotNET Jun 08 '14 at 01:48
2

There's a lot of confusion and misconceptions around REST. Unfortunately, it's a lot more common to find applications that are doing the exact opposite of what REST means and calling themselves RESTful than real REST applications.

From what I understand it is just a convention for writing the functions within the API?

No, REST is not just a convention for writing functions within the API, nor it's directly related to SOAP or HTTP as other answers here say. REST is a software architectural style inspired by the successful design decisions made for the web itself. To put it in simple terms, a REST API should work like a website does.

When you enter a website, you go to a homepage having an idea what the website is about, and the HTML document will have hyperlinks pointing you to the resources you need. The only out-of-band information are the media-types of the resources themselves, not how to find them. For instance, when you enter StackOverflow, you know what questions and answers are, and you look for links pointing you to them. How your browser render those links, how you choose them and follow them isn't different from other websites, like an email or news website. What makes it different is the media-type of the resources you're after.

That's how a REST API should work. Clients should not depend on any out-of-band information other than detailing what the resources do. They should connect to a home page that returns them links they should follow to do whatever they need. If an API doesn't do this, then it's not REST. Period.

I like to call those APIs "street-REST", because people often implement them by copying what they see in other APIs that also call themselves REST, and by what other people talk about REST.

All functions should be of either GET/POST/DELETE/PUT form?

That's a common confusion, and you'll see a lot of that, including people conflating that to CRUD operations, or that REST doesn't allow any other verbs, etc. That's bull.

REST is independent of any particular protocol, so it doesn't make sense to say functions should follow HTTP methods. REST constrain your applications into an uniform interface, meaning that whatever protocol you should using, you must stick to the standardized behavior as much as possible. If you're implementating a REST application over HTTP, this means your API must stick to the HTTP methods for the client-server interaction, meaning you can't invent other HTTP methods as some applications using HTTP do. If you change the communication protocol, clients need to know that information before entering your API, and that's more out-of-band information.

How you implement this on your code is irrelevant to REST. REST isn't a development pattern or philosophy, but an architectural style.

I am mainly confused about how JSON/XML play a role in this?

There's a lot of confusion on this too. On a REST application you should define abstract entities with states describing all the behavior you need. The API will serve as a channel to transfer media-type representations of those entities between client and server. REST means Representational State Transfer. The URI the client is requesting is an identifier for that resource, and the metadata for that request will tell the server what media-types the client is prepared to accept. JSON/XML don't play any direct role in REST, at all. They are simply one representation media-type that is easier for computers to parse and obtain information, in contrast to formats like text/html, which is meant to be rendered for human visualization by a browser.

For example, take StackOverflow itself. What's a question, in the abstract sense? It's a request for information. How that abstract resource is formally defined? There's an author, there's the actual text of the question being asked, there's the upvotes and downvotes, the comments and possible answers, etc, all of which are also abstract resources with their own definitions. The actual data is stored in a database somewhere, and when you request your homepage, it returns links with URIs identifying those questions. Take this question for instance, it has the URI http://stackoverflow.com/questions/24092517. When I want to read that question in a pretty document rendered on my browser, I will request that URI, but telling the server through the Accept header that I want a text/html representation, and my browser knows how to render the HTML into a pretty page. On the other hand, when I want to request that question to store it back on a database, for instance, I don't need all the cute stuff needed to render a pretty document page, so I ask a format that's easier to parse and doesn't contain a lot of unnecessary information, like JSON or XML.

Most people who build street-REST APIs understand this point, but they miss the most interesting part, which is that you're not limited to media-types that already exist. You can create private media-types that only exist within your API, or your company's ecosystem of applications. So, for instance, instead of calling the media type of all JSON documents application/json, no matter the content of them, we could have custom media-types that reflect more accurately that particular type of resource. So, we could have a application/vnd.stackoverflow.question.v1+json for StackOverflow questions represented in a JSON format. Once you do that, instead of wasting time documenting operations already standardized by the communication protocol, you should focus on documenting that custom media-type and how to interact with it, independently of the communication protocol. Once you have that clear, clients can interact with your services using any protocol available.

If you understand these three main points, you understand what REST is about. By using hyperlinks as the engine of your application state you're not tied to any particular point in time of your implementation. Your server can evolve at will, you can change URIs as much as you want, and clients won't break. By sticking to the standardized protocol, it's easier for generic clients to make use of your API, not to mention that it's easier for developers to understand how to integrate if they already know that you won't break the protocol. By focusing your design and documentation efforts on your media-types, not on protocol details and URI semantics, you're avoiding introducing more out-of-band information needed to drive your API, and your clients are also more resilient to changes.

Pedro Werneck
  • 40,902
  • 7
  • 64
  • 85
  • A very well put explanation. However, I'm not sure how *useful* this is given that (quoting from MSDN) "Although in theory REST isn’t tied to any specific platform or technology, the Web is the only major platform that fully embodies REST today. So, in practical terms, if you’re going to build something that’s RESTful today, you’ll probably do it on the Web using HTTP." We (generally) are not theorists on StackOverflow, we are looking for **practical** programming knowledge. In practice, REST is very much tied to HTTP. – BradleyDotNET Jun 07 '14 at 23:58
  • Sorry, I've seen the "practical" or "pragmatic" argument before, and it doesn't hold water. If you're saying that in the sense that this is what people do in practice and they call it REST, then fine, it's practical, but it's just a buzzword. Street-REST is not practical, and it's not effective in tackling the same issues REST intends to address. It solves another set of problems. If you want to call that REST, I'm not going to stop you, but when someone asks what exactly a REST-based API is, I think he deserves the correct answer. – Pedro Werneck Jun 08 '14 at 00:34
  • Never said it wasn't an answer :) I think the combination of both pieces of information are important, I just wish there was an easy way to take the theory and make it into practical information for those that want to use a REST service (or perhaps we should just come up with a new name) – BradleyDotNET Jun 08 '14 at 00:47
0

REST API's act as middleman to deliver data packets between the web service and an application wanted to use some resource from web service for its operations, in order to do so Json comes into play, which helps in data transfer/communication over the channel. When one's application wishes to get list of resource, it actually get complex data or queryset from database which turn into simple data type with serialization and then turn into json to travers the channel.

Amit Verma
  • 11
  • 1
  • 2
0

RESTful API is much more than just the convention of writing functions.

The abbreviation REST stands for "REpresentational State Transfer".

REST APIs are used to call resources and allow the software to communicate based on standardized principles, properties, and constraints. REST APIs operate on a simple request/response system. You can send a request using these HTTP methods:

  • GET
  • POST
  • PUT
  • PATCH
  • DELETE

Also, REST APIs can include endpoints, headers, URL parameters, and the request body.

The endpoint (or route) is the URL you request for. The path determines the resource you’re requesting for. Think of it like an automated answering machine that asks you to press 1 for service, press 2 for another service, 3 for yet another service, and so on.

I am mainly confused about how JSON/XML play a role in this?

When you send a request to an endpoint, it responds with the relevant data, which is generally formatted as JSON, XML, plain text, images, HTML, and more.

Pratham
  • 497
  • 3
  • 7
-1

I am mainly confused about how JSON/XML play a role in this?

JSON/XML are called streaming data format. There are others but over the years these two became so popular because of low latency they provide. XML is probably still little bit more popular than JSON, but JSON is more compact.

Also another main reason to use them is because they are both supported by almost all languages and their frameworks.

Md Nazmoon Noor
  • 3,187
  • 1
  • 24
  • 30
  • 1
    I'm curious where you get the term "streaming data format" I don't believe there is anything inherently "streamed" about them. I would also claim that on the web, JSON is far more popular. XML is used almost exclusively for "local" purposes unless you count WCF. – BradleyDotNET Jun 07 '14 at 02:00
  • I guess it never was set in stone but some cases been used. http://java.dzone.com/articles/streaming-apis-json-vs-xml-and – Md Nazmoon Noor Jun 07 '14 at 02:08
  • I think you are confusing streaming APIs (which is still a poor name IMO) with the data types. The article never claims the data types are streamed. An interesting read though, thank you for the link! – BradleyDotNET Jun 07 '14 at 04:08