Hello readers, in this article we will discuss one of the most important parts of programming, i.e. API. Also, we will discuss REST API eg. GET requests, POST requests, PATCH requests, etc.
Contents
What is the full form of API?
API stands for Application Programming Interface. It acts as a bridge between the user node and the database.
What is an API?
According to Wikipedia, An application programming interface (API) is a connection between computers or between computer programs. It is a type of software interface, that offers a service to other pieces of software. A document or standard that describes how to build or use such a connection or interface is called an API specification. A computer system that meets this standard is said to implement or expose an API. The term API may refer either to the specification or to the implementation.
In contrast to a user interface, which connects a computer to a person, an application programming interface connects computers or pieces of software to each other. It is not intended to be used directly by a person (the end-user) other than a computer programmer who is incorporating it into the software. An API is often made up of different parts which act as tools or services that are available to the programmer. A program or a programmer that uses one of these parts is said to call that portion of the API. The calls that make up the API are also known as subroutines, methods, requests, or endpoints. An API specification defines these calls, meaning that it explains how to use or implement them.
One purpose of APIs is to hide the internal details of how a system works, exposing only those parts a programmer will find useful and keeping them consistent even if the internal details later change. An API may be custom-built for a particular pair of systems, or it may be a shared standard allowing interoperability among many systems.
The term API is often used to refer to web APIs, which allow communication between computers that are joined by the internet. There are also APIs for programming languages, software libraries, computer operating systems, and computer hardware. APIs originated in the 1940s, though the term did not emerge until the 1960s and 1970s.
Different types of requests
In REST API, there are different types of requests. The most used and most common requests are listed below:
- GET request
- POST request
- DELETE request
- PATCH request
GET Request
GET request is the default that you see when you open an app. Whenever you visit a URL, it automatically treats it as a GET request. Hence, the GET request is the default request. Also, a GET request is used to fetch data from an API.
POST Request
A POST request is used to add data to the database through forms. You can use POST requests in HTML form. So, if you want to fill data into the database you will be using a POST request.
DELETE Request
A DELETE request is used to delete data from the database as the name suggests. When you call the DELETE request the data gets deleted.
PATCH Request
A PATCH request is used to update data into the database. As the name suggests PATCH, this makes corrections in the data.
The CRUD operation
CRUD stands for Create, Read, Update, Delete. In order to call an application full-stack, the application should consist of a CRUD operation. In order to perform CRUD operation, the above-mentioned requests are enough as POST request handles create, GET request handles read, PATCH request handles update, and DELETE request handles delete.
It looks like the following in a MERN stack application.
const express = require(“express”); const router = express.Router(); const Post = require(“../models/Post”); //get all post router.get(“/”, async (req, res) => { try { const posts = await Post.find(); res.json(posts); } catch (err) { console.log(err.message); } }); //create operation router.post(“/”, async (req, res) => { const post = new Post({ title: req.body.title, description: req.body.description, thumbnail: { data: fs.readFileSync( path.join(__dirname + “/uploads/” + req.file.filename) ), contentType: “image/png”, }, slug: req.body.slug, }); try { await post.save().then((data) => { res.json(data); }); } catch (err) { console.log(err.message); } }); //get specific id router.get(“/:slug”, async (req, res) => { try { const post = await Post.find({ slug: { $eq: req.params.slug } }); res.json(post); } catch (err) { console.log(err.message); } }); //delete post router.delete(“/:PostId”, async (req, res) => { try { const DeletedPost = await Post.remove({ _id: req.params.PostId }); res.json(DeletedPost); } catch (err) { console.log(err.message); } }); //update post router.patch(“/:PostId”, async (req, res) => { try { const UpdatedPost = await Post.updateOne( { _id: req.params.PostId }, { $set: { title: req.body.title, description: req.body.description, thumbnail: req.body.thumbnail, slug: req.body.slug, tags: req.body.tags, }, } ); res.json(UpdatedPost); } catch (err) { console.log(err.message); } }); module.exports = router;