\

Understanding the RESTful API Methods

Understanding the RESTful API Methods

In this tutorial we will be Understanding the RESTful API Methods i.e. GET POST PUT DELETE


Check out the example of RESTful API HTTP methods.



What Is RESTful API ?

A RESTful API is an application program interface (API) that uses HTTP requests to GET, PUT, POST and DELETE data. Representational state transfer (REST), which is used by browsers, can be thought of as the language of the Internet.

REST is the underlying architectural principle of the web. The amazing thing about the web is the fact that clients (browsers) and servers can interact in complex ways without the client knowing anything beforehand about the server and the resources it hosts. The key constraint is that the server and client must both agree on the media used, which in the case of the web is HTML.



Guidelines For RESTful API

The 5 basic design guidelines that make a RESTful API

  • Resources (URIs)
  • HTTP methods
  • HTTP headers
  • Query parameters
  • Status codes

HTTP methods In REST API

The 4 basic method In RESTful API

  • GET
  • POST
  • PUT
  • DELETE



The GET method

The HTTP GET method is used to read (or retrieve) a representation of a resource. In the “happy” (or non-error) path, GET returns a representation in XML or JSON and an HTTP response code of 200 (OK). In an error case, it most often returns a 404 (NOT FOUND) or 400 (BAD REQUEST)

Example of GET method through url :

GET http://www.example.com/api/feedback/getAllUsers

Code example

router.get('/getAll', function(req, res) {
  users.getAllUsers(function(err, data) {
    if(err){
      res.json({ 'error': true, 'message': "Error fetching users" });
    }else{
      res.json({ 'success': true, 'users': data });
    }
  });
});

The POST method

The POST verb is most-often utilized to create new resources. In particular, it’s used to create subordinate resources. That is, subordinate to some other resource. In other words, when creating a new resource, POST to the parent and the service takes care of associating the new resource with the parent, assigning an ID, etc.

The POST method is used to request that the origin server accept the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI in the Request-Line. POST is designed to allow a uniform method to cover the following functions:

  • Annotation of existing resources
  • Posting a message to a bulletin board, newsgroup, mailing list, or similar group of articles
  • Providing a block of data, such as the result of submitting a form, to a data-handling process
  • Extending a database through an append operation.


Example of POST method through url

http://localhost:3000/api/users/login

Code example

router.post('/login', function(req, res) {
  login.loginUser(req, res, function(err, data) {
    if (err) {
      res.json({ 'error': true, 'message': 'Error logged in' });
    } else {
      res.json({ 'success': true, 'data': data });
    }
  });
});

The PUT method

PUT is most-often utilized for update capabilities, PUT-ing to a known resource URI with the request body containing the newly-updated representation of the original resource. PUT method is for creating or replacing a resource at a URL known by the client.

However, PUT can also be used to create a resource in the case where the resource ID is chosen by the client instead of by the server. In other words, if the PUT is to a URI that contains the value of a non-existent resource ID. Again, the request body contains a resource representation.


Code example

router.put('/profile', function(req, res) {
  profile.updateProfile(req, res, function(err, data) {
    if (err) {
      res.json({ 'error': true, 'message': 'Error updating profile' });
    } else {
      res.json({ 'success': true, 'message': 'profile update succesfully' });
    }
  });
});

The DELETE method

Delete methode removes all current representations of the target resource given by a URI.
On successful deletion, return HTTP status 200 (OK) along with a response body, perhaps the representation of the deleted item (often demands too much bandwidth), or a wrapped response (see Return Values below). Either that or return HTTP status 204 (NO CONTENT) with no response body. In other words, a 204 status with no body, or the JSEND-style response and HTTP status 200 are the recommended responses.


Example delete an feedback with an ID of 1:

DELETE /feedback/1

Code example

router.delete('/delete/:id', function(req, res) {
  deleteFeedback.deleteFeedback(req, res, function(err, data) {
    if (err) {
      res.json({ 'error': true, 'message': 'Error deleting records' });
    } else {
      res.json({ 'success': true, 'message': 'Deleted succesfully' });
    }
  });
});
About This Author

Hi This Is Rehmaanali. Blogging Is Just A Hobby. Graduate In Bsc Computer Science

Post A Reply