Please enable / Bitte aktiviere JavaScript!
Veuillez activer / Por favor activa el Javascript![ ? ]
What is RESTful programming? - bug-code.com Q&A

What is RESTful programming?

0 like 0 dislike
38 views
What is RESTful programming?
asked by (1.3k points) | 38 views

1 Answer

0 like 0 dislike

REST is the fundamental architectural principle of the network. The most surprising thing on the Internet is that clients (browsers) and servers can interact in a complex way, and the client does not know anything in advance about the server and the resources that it hosts. A key limitation is that the server and client must both agree to the media used, which in the case of the web is HTML.

An API that adheres to the principles of REST does not require the client to know anything about the structure of the API. Most likely, the server should provide any information necessary for the client to interact with the service. An example of this is the HTML form: the server indicates the location of the resource and the required fields. The browser does not know in advance where to send information, and it does not know in advance what information to send. Both forms of information are fully provided by the server. (This principle is called HATEOAS: hypermedia as the engine of application state.)

So how does this relate to HTTP and how can this be implemented in practice? HTTP is focused on verbs and resources. The two verbs in common use are GET and POST, which I think everyone will recognize. However, the HTTP standard defines several others, such as PUT and DELETE. These commands are then applied to the resources in accordance with the instructions provided by the server.

For example, let's say we have a user database managed by a web service. Our service uses JSON-based custom hypermedia, for which we assign mimetype application / json + userdb (there can also be application / xml + userdb and application / whatever + userdb - many types of media can be supported). The client and server have been programmed to understand this format, but they do not know anything about each other. As Roy Fielding notes:

A REST API should spend almost all of its descriptive efforts in determining the type (s) of media used to represent and manage the state of the application or when defining extended relationship names and / or hypertext margins for existing standard media types.

A request for a base resource / can return something like this:

Request
 

GET /
Accept: application/json+userdb



Answer .
 

200 OK
Content-Type: application/json+userdb

{
    "version": "1.0",
    "links": [
        {
            "href": "/user",
            "rel": "list",
            "method": "GET"
        },
        {
            "href": "/user",
            "rel": "create",
            "method": "POST"
        }
    ]
}



We know from the description of our media that we can find information about related resources from sections called "links". It's called hyper-media control. In this case we can tell from the section that we can find the list of users by making another request to /user :

Request
 

GET /user
Accept: application/json+userdb



Answer .
 

200 OK
Content-Type: application/json+userdb

{
    "users": [
        {
            "id": 1,
            "name": "Emil",
            "country: "Sweden",
            "links": [
                {
                    "href": "/user/1",
                    "rel": "self",
                    "method": "GET"
                },
                {
                    "href": "/user/1",
                    "rel": "edit",
                    "method": "PUT"
                },
                {
                    "href": "/user/1",
                    "rel": "delete",
                    "method": "DELETE"
                }
            ]
        },
        {
            "id": 2,
            "name": "Adam",
            "country: "Scotland",
            "links": [
                {
                    "href": "/user/2",
                    "rel": "self",
                    "method": "GET"
                },
                {
                    "href": "/user/2",
                    "rel": "edit",
                    "method": "PUT"
                },
                {
                    "href": "/user/2",
                    "rel": "delete",
                    "method": "DELETE"
                }
            ]
        }
    ],
    "links": [
        {
            "href": "/user",
            "rel": "create",
            "method": "POST"
        }
    ]
}



We can say a lot from that answer. For example, we now know that we can create a new user using POST ing to /user :

Request
 

POST /user
Accept: application/json+userdb
Content-Type: application/json+userdb

{
    "name": "Karl",
    "country": "Austria"
}



Answer .
 

201 Created
Content-Type: application/json+userdb

{
    "user": {
        "id": 3,
        "name": "Karl",
        "country": "Austria",
        "links": [
            {
                "href": "/user/3",
                "rel": "self",
                "method": "GET"
            },
            {
                "href": "/user/3",
                "rel": "edit",
                "method": "PUT"
            },
            {
                "href": "/user/3",
                "rel": "delete",
                "method": "DELETE"
            }
        ]
    },
    "links": {
       "href": "/user",
       "rel": "list",
       "method": "GET"
    }
}


We also know that we can change existing data:

Request
 

PUT /user/1
Accept: application/json+userdb
Content-Type: application/json+userdb

{
    "name": "Emil",
    "country": "Bhutan"
}



Answer .
 

200 OK
Content-Type: application/json+userdb

{
    "user": {
        "id": 1,
        "name": "Emil",
        "country": "Bhutan",
        "links": [
            {
                "href": "/user/1",
                "rel": "self",
                "method": "GET"
            },
            {
                "href": "/user/1",
                "rel": "edit",
                "method": "PUT"
            },
            {
                "href": "/user/1",
                "rel": "delete",
                "method": "DELETE"
            }
        ]
    },
    "links": {
       "href": "/user",
       "rel": "list",
       "method": "GET"
    }
}

Please note that we use different HTTP verbs (GET, PUT, POST, DELETE, etc.) to manipulate these resources, and that the only knowledge that we assume from the client side is our definition of media.

Further reading:

    Many much better answers on this very page.
    As I explained to my wife REST.
    As I explained to my wife REST.
    Martin Fowler's thoughts
    PayPal API has hypermedia management

(This answer was the subject of a fair amount of criticism for missing the point. For the most part, it was fair criticism. What I originally described was more in line with how REST was usually implemented several years ago when I first wrote this, and not its true meaning. I revised the answer to better represent the real meaning.)

answered by (700 points)
edited by
Welcome to Bug-code Q&A, where you can ask questions and receive answers from other members of the community.

24.8k questions

46.2k answers

0 comments

12.7k users

24,788 questions
46,220 answers
0 comments
12,685 users