Lets Talk About REST API`s .

Avinash Yadav
3 min readJun 30, 2020

--

In this we’re going to talk about RESTful API.

Now what is that?

A REST API defines a set of functions which developers can perform requests and receive responses via a HTTP protocol such as GET, POST, PUT, DELETE .

Remember a web browser sends a request and just hopes that the server returns what the browser asked for.

A Google server can send us whatever it wants.It doesn’t have to follow the rules that perhaps the web browser sets. With RESTful API we’re able to create an API that is RESTful — Something that follows the rules that everybody can agree on, so that we have compatibility between different systems.

So a RESTful API is an architectural style and it’s an approach to communications to agreed upon set of rules .

A RESTful API users GET-to receive a resource, PUT-to change the state or update a resource, a POST that creates a resource, and a DELETE to remove it. and the RESTful API is a way to define our server so that it specifies what it can provide and how to use it.

In other words the URL parameters should make sense.

For example if we’re doing ‘/profile’ well, we expect that if it’s a GET request we’re going to get a profile.

If it’s a POST request we’re going to add a profile.

If it’s a PUT perhaps we’re updating the profile, and this URL makes sense.

Now finally REST APIs are something called ‘stateless’ — meaning that calls can be made independently of one another and each call contains all the data necessary to complete itself successfully.

A server doesn’t need to keep memorizing things.

Each request that comes in has enough information that the server can respond with, regardless of who that person is.

So in this example let’s build a small little app that has a RESTful API.

Well when we look at a GET request ,this GET request will have a request object that we receive.

Now this request object can have a few things —

we can have req.query , req.body , req.header and req.params.

1.example of req.query -

when we go to browser localhost:3000/ ? name = avinash ? age = 23 & we console.log(req.query);

o/p : { name : ’avinash’ , age: ’23’ }

2. We also have req.body -using something like your ‘urlencoded’ or ‘JSON’ body-parsers, we can add that middleware to receive whatever the request sends in the body.

3.We also have req.header.

So if we console.log(req.header);

We can use the Postman and in the header section Put the Header

for eg : name Avinash

we will O/p as ‘Name’: ‘Avinash’.

4.We also have req.params .

when we put the URL in the browser

localhost:3000/1234

we will get the output : { id : ‘1234’ }.

--

--