REST API
#Node JS Notes
What is API ?
A web service is a collection of open protocols and standards used for
exchanging data between applications or systems.
Software applications written in various programming languages and running
on various platforms can use web services to exchange data over computer
networks like the Internet in a manner similar to inter-process
communication on a single computer.
Akash Technolabs www.akashsir.com
2
What is API ?
API (Application Program Interface) is an agreed way to send and receive
data between computers.
For example, if you want to display Google Maps on your site, but the maps
are on Google's servers, you need a way to ask Google to provide you with
the maps.
The way to ask Google to send you the requested maps is through an API
provided by Google that tells you to which web addresses should you send
the requests to get the data.
In a more formal language, you need to send a request to the remote server
to get a response.
Akash Technolabs www.akashsir.com
3
What is Rest?
REST (Representational State Transfer) is an API that defines a set of
functions that programmers can use to send requests and receive responses
using the HTTP protocol methods such as GET and POST.
Akash Technolabs www.akashsir.com
4
What is REST API ?
REST API can be used by any site or application no matter what language it is
written in because the requests are based on the universal HTTP protocol,
and the information is usually returned in the JSON format that almost all of
the programming languages can read.
Akash Technolabs www.akashsir.com
5
Akash Technolabs www.akashsir.com
6
HTTP methods
Following four HTTP methods are commonly used in REST based
architecture.
GET − Retrieves data from a remote server. It can be a single resource or a list
of resources.
POST − Creates a new resource on the remote server.
PUT − Updates the data on the remote server.
DELETE − Deletes data from the remote server.
Akash Technolabs www.akashsir.com
7
HTTP status codes
Code Message Description
200 OK The data was received and the operation was performed.
The data was received and a new resource was created. The response
201 Created
needs to return the data in the payload.
The operation was successful but no data is returned in the response
204 No content
body. This is useful when deleting a resource.
301 Moved permanently This and all the future requests should be redirected to a new URL.
302 Moved temporarily The resource moved temporarily to another URL.
The server cannot accept the request because something is wrong
400 Bad request
with the client or the request that it sent.
403 Forbidden The client is not allowed to use the resource.
404 Not found The computer is not able to find the resource.
For example, when sending a DELETE request to a server that doesn't
405 Method not allowed
support the method.
500 Internal server error Service unavailable due to error on the server side.
Akash Technolabs www.akashsir.com
8
For example, to create a new article on our blog, we should send a request to
a remote server using the POST HTTP method.
To view a single article or a list of articles, we use the GET method. The PUT
method can be used to edit an existing article, and the DELETE method to
delete.
Method Url Action
GET users fetch all users
GET
user/1 fetch user with id =1
POST user add new user
PUT user/1 update user by id = 1
DELETE user/1 delete user by id = 1
Akash Technolabs www.akashsir.com
9
GET /photo — Retrieve all photos
GET /photo/:id — Retrieve a photo by its ID
POST /photo — Create a new photo
PUT /photo/:id — Update properties of a photo by its ID
DELETE /photo/:id — Delete a photo by its ID
Akash Technolabs www.akashsir.com
10
Software to Test API
PostMan
https://www.postman.com/downloads/
VS Code (Thunder Client)
https://marketplace.visualstudio.com/items?itemName=rangav.vscode-thunder-client
https://www.thunderclient.io/
JSON Viewer Chrome Extenstion (Get Method)
https://chrome.google.com/webstore/detail/json-viewer/gbmdgpbipfallnflgajpaliibnhdgobh
Akash Technolabs www.akashsir.com
11
JSON Validator
https://jsonlint.com/
https://jsonformatter.org/
https://jsonformatter.curiousconcept.com/
https://elmah.io/tools/json-formatter/
https://codebeautify.org/jsonvalidator
Akash Technolabs www.akashsir.com
12
Mock (Fake) JSON Data
https://jsonplaceholder.typicode.com/
https://www.mockaroo.com/
https://fakestoreapi.com/
https://www.npmjs.com/package/json-server
https://www.mockachino.com/
https://dummy.restapiexample.com/
https://gorest.co.in/
Akash Technolabs www.akashsir.com
13
JSON DATA Pass
Simple Json Data Convert
res.send(JSON.stringify(rows));
Json Data with Status Code
res.status(200).send(JSON.stringify(rows));
Json Data with Status Code Error
res.status(500).send(JSON.stringify({"msg":"Error"}));
Akash Technolabs www.akashsir.com
14
Json Data with Object and Array
res.send(JSON.stringify({"status": 200,"flag": 1, "message": "Data Fetch", "data": rows}));
res.send(JSON.stringify({"status": 500,"flag": 0, "message": "Error", "data": err}));
Json Data with Status Code a Object and Array
res.status(500).send(JSON.stringify({"status": 500,"flag": 0, "message": "Error", "data": err}));
res.status(200).send(JSON.stringify({"status": 200,"flag": 1, "message": "Data Deleted", "data": ''}));
Akash Technolabs www.akashsir.com
15
MongoDB
Akash Technolabs www.akashsir.com
16
Display
Akash Technolabs www.akashsir.com
17
Display
Akash Technolabs www.akashsir.com
18
Add Record
Akash Technolabs www.akashsir.com
19
Add Record
Akash Technolabs www.akashsir.com
20
Single Record
Akash Technolabs www.akashsir.com
21
Akash Technolabs www.akashsir.com
22
Delete
Akash Technolabs www.akashsir.com
23
Akash Technolabs www.akashsir.com
24
Update
Akash Technolabs www.akashsir.com
25
Akash Technolabs www.akashsir.com
26
API Create using
Mysql
Akash Technolabs www.akashsir.com
27
Table Structure
Akash Technolabs www.akashsir.com
28
Record
Akash Technolabs www.akashsir.com
29
Mysql Connection
Akash Technolabs www.akashsir.com
30
Get all User Data
Akash Technolabs www.akashsir.com
31
Get All Record in Array (Postman)
Akash Technolabs www.akashsir.com
32
Chrome Output
Akash Technolabs www.akashsir.com
33
Get All Record with Object
Akash Technolabs www.akashsir.com
34
Get All Record with Object
Akash Technolabs www.akashsir.com
35
Akash Technolabs www.akashsir.com
36
Akash Technolabs www.akashsir.com
37
Check Data Present or not and Print Counter
Akash Technolabs www.akashsir.com
38
Akash Technolabs www.akashsir.com
39
Akash Technolabs www.akashsir.com
40
Add Record
Akash Technolabs www.akashsir.com
41
https://learning.postman.com/docs/sending-requests
Akash Technolabs www.akashsir.com
42
Akash Technolabs www.akashsir.com
43
What is the difference between form-data, x-www-form-
urlencoded and raw
These are different Form content types defined by W3C. If you want to
send simple text/ ASCII data, then x-www-form-urlencoded will work.
This is the default.
But if you have to send non-ASCII text or large binary data, the form-
data is for that.
You can use Raw if you want to send plain text or JSON or any other
kind of string. Like the name suggests, Postman sends your raw string
data as it is without modifications. The type of data that you are
sending can be set by using the content-type header from the drop
down.
Binary can be used when you want to attach non-textual data to the
request, e.g. a video/audio file, images, or any other binary data file.
Akash Technolabs www.akashsir.com
44
Update
Akash Technolabs www.akashsir.com
45
Put and Patch
PUT is a method of modifying resource where the client sends data that
updates the entire resource .
PATCH is a method of modifying resources where the client sends partial data
that is to be updated without modifying the entire data.
Akash Technolabs www.akashsir.com
46
Akash Technolabs www.akashsir.com
47
Akash Technolabs www.akashsir.com
48
Delete Record
Akash Technolabs www.akashsir.com
49
Akash Technolabs www.akashsir.com
50
Akash Technolabs www.akashsir.com
51
Akash Technolabs www.akashsir.com
52
Cors
Akash Technolabs www.akashsir.com
53
What is Cors ?
Cross-Origin Resource Sharing (CORS) is a mechanism that uses additional
HTTPheaders to tell a browser to let a web application running at one origin
(domain) have permission to access selected resources from a server at a
different origin
Akash Technolabs www.akashsir.com
54
Akash Technolabs www.akashsir.com
55
Akash Technolabs www.akashsir.com
56
Akash Technolabs www.akashsir.com
57
Akash Technolabs www.akashsir.com
58
Akash Technolabs www.akashsir.com
59
Enjoy
Akash Technolabs www.akashsir.com
60
Get Exclusive
Video Tutorials
www.aptutorials.com
https://www.youtube.com/user/Akashtips
www.akashsir.com
Rating Us Now
Just Dial
https://www.justdial.com/Ahmedabad/Akash-Technolabs-
Navrangpura-Bus-Stop-Navrangpura/079PXX79-XX79-
170615221520-S5C4_BZDET
Sulekha
https://www.sulekha.com/akash-technolabs-navrangpura-
ahmedabad-contact-address/ahmedabad
Connect With Me
# Social Info
Akash.padhiyar
Akashpadhiyar
Akash Padhiyar
#AkashSir Akash_padhiyar
+91 99786-21654
www.akashsir.com
www.akashtechnolabs.com #Akashpadhiyar
www.akashpadhiyar.com #aptutorials
www.aptutorials.com