Rest API
Rest API
REST (Representational State Transfer) is an architectural style for designing networked applications.
RESTful APIs, often simply called REST APIs, are a set of rules and conventions for building and
interacting with web services.
1. **Resources:**
2. **HTTP Methods:**
- Common HTTP methods include GET (retrieve data), POST (create a new resource), PUT (update
an existing resource), and DELETE (remove a resource).
3. **Stateless Communication:**
- REST is stateless, meaning each request from a client to a server must contain all the information
needed to understand and fulfill that request.
- The server doesn't store any information about the client between requests.
4. **Representation:**
- Resources are represented in different formats, such as JSON (JavaScript Object Notation) or XML
(eXtensible Markup Language).
5. **Uniform Interface:**
- RESTful APIs have a uniform and consistent interface, making it easier for clients to understand
how to interact with resources.
6. **Stateless Communication:**
- RESTful APIs follow a client-server architecture where the client and server are independent
entities.
- Clients are not concerned with the server's state, and servers are not concerned with the client's
state.
**Example:**
- **Resource:** `/books`
- **HTTP Methods:**
Clients can interact with these endpoints using the defined HTTP methods, and the server responds
with representations of the resources (e.g., book details in JSON format).
In summary, a REST API is a set of conventions and principles for designing web services that use
standard HTTP methods and a stateless communication model. It provides a uniform and consistent
way for clients to interact with resources over the web.
RESTAPI stands for Representational State Transfer Application Programming Interface. It is a set of
rules and conventions for building and interacting with web services. REST is an architectural style
that uses standard HTTP methods (such as GET, POST, PUT, and DELETE) and is based on a client-
server model where communication is stateless. Here are some key principles of RESTful APIs:
1. **Statelessness:** Each request from a client to a server must contain all the information needed
to understand and fulfill that request. The server should not store any information about the client's
state between requests.
2. **Client-Server Architecture:** The client and server are separate entities that communicate over
a network. The client is responsible for the user interface and user experience, while the server is
responsible for processing requests and managing resources.
3. **Uniform Interface:** A uniform and consistent way to interact with resources is provided. This
includes a common set of conventions for resource identification, resource representation, and
resource manipulation.
4. **Resource-Based:** Resources are the key abstractions in a RESTful API. Each resource is
identified by a unique URI (Uniform Resource Identifier), and interactions with resources are
performed using standard HTTP methods.
5. **Representation:** Resources can have different representations, such as JSON or XML. Clients
interact with resources by exchanging representations.
6. **Stateless Communication:** Each request from a client to a server must contain all the
information needed to understand and fulfill that request. The server should not store any
information about the client's state between requests.
7. **HATEOAS (Hypermedia As The Engine Of Application State):** The server provides hypermedia
links in the response to guide the client on the possible actions it can take next. This helps in making
the API discoverable and self-explanatory.
```
GET /api/users/123
```
In this example:
- `/api/users/123` is the URI that identifies the resource (in this case, user with ID 123).
RESTful APIs are commonly used in web development to enable communication between different
systems, allowing them to exchange data and perform actions over the web. They are widely used
for building web services and are the foundation of many modern web applications.
NO.
Layered system
All components will support starnard protocol for communication ( predefined interface )
This layers are same be added removed modified accoreing to the needs , we can rearrange the
rechitecture for sacling.
CACHE :
Helps to reduce the load on server and server latency because no need to prepare data again and
again.
Proxy cache
Statelessness :
Ex: watching youtube video and close tab and open again it will r4esume at same point .
EXAMPLE :
Statelessness in the context of RESTful APIs means that each request from a client to a server must
contain all the information needed to understand and fulfill that request. The server should not
store any information about the client's state between requests. In other words, each request
should be independent and self-contained.
Let's consider an example to illustrate statelessness. Suppose we have a simple web application that
manages a list of tasks. Each task has an ID, a title, and a status (e.g., "completed" or "in progress").
The client interacts with the server through a RESTful API to perform operations on tasks.
1. **Creating a Task:**
- Request:
```http
POST /api/tasks
Content-Type: application/json
```
- In this example, the client sends a request to create a new task. The request contains all the
necessary information (task title and status).
2. **Fetching a Task:**
- Request:
```http
GET /api/tasks/1
```
- The client requests information about a specific task with ID 1. The server processes the request
based solely on the provided URI and doesn't rely on any previous interactions with the client.
3. **Updating a Task:**
- Request:
```http
PUT /api/tasks/1
Content-Type: application/json
"status": "completed"
```
- The client sends a request to update the status of the task with ID 1. The request contains all the
information needed for the server to perform the update.
4. **Deleting a Task:**
- Request:
```http
DELETE /api/tasks/1
```
- The client requests to delete the task with ID 1. The server can process this request without
relying on any previous state information.
In each of these examples, the server processes the requests based solely on the information
provided in the request itself. There is no session state or context stored on the server between
requests. This adherence to statelessness makes the API more scalable and easier to maintain, as
each request is independent and doesn't rely on a shared context between the client and server.
Let's consider a weather service that provides information about the current weather conditions for
a given location. This weather service exposes a web service API that allows other applications to
retrieve weather information.
```
GET /api/weather?location=city1
```
- Another application, let's say a mobile weather app, wants to display the current weather for
"city1." The app sends a request to the weather service's API endpoint.
```http
GET /api/weather?location=city1
```
- The weather service processes the request and sends back a response containing the weather
information for "city1." The response is typically in a standardized format, such as JSON or XML.
```json
"location": "city1",
"temperature": 25,
```
- The weather app receives the response from the weather service and can then use the provided
data to display the current temperature and weather conditions for "city1" to the user.
In this example:
- The weather service acts as a web service, providing a standardized API (in this case, a RESTful API)
for accessing weather information.
- The client application communicates with the web service by sending HTTP requests to the
specified API endpoint.
- The web service processes the request, retrieves the relevant data, and returns it to the client in a
structured format.
- The client application consumes the data and uses it to enhance its functionality, such as displaying
real-time weather information to users.
This scenario demonstrates the fundamental concept of a web service, where different applications
can interact and share data over the web in a standardized and interoperable manner.
Service : it is software or piece of code , that performs specific tasks or response to event as per
request and product specific output :
Web service is avalible in diffenent machine (server) and client machine request fro this service over
internet and receives output.
It is set of definations and protocols that allows one application to communicate with another.
It is not necessary that API means web API , we have API with local files
Interface means it is a edge or a boundary where two system can meet and exchange the data or
information
1. **RESTful API:**
```
GET /api/books/123
```
- **Response:**
```json
"id": 123,
"genre": "Fiction"
```
2. **JSON-RPC API:**
- JSON-RPC is a remote procedure call (RPC) protocol encoded in JSON. It allows clients to
make requests and receive responses in JSON format. Example using a calculator API:
```json
"jsonrpc": "2.0",
"method": "add",
"id": 1
```
- **Response:**
```json
"jsonrpc": "2.0",
"result": 8,
"id": 1
```
3. **SOAP API:**
- SOAP (Simple Object Access Protocol) is a protocol for exchanging structured information
in web services. It typically uses XML for message formatting. Example using a currency conversion
service:
- **SOAP Request:**
```xml
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:web="http://www.example.com/webservice">
<soap:Header/>
<soap:Body>
<web:ConvertCurrency>
<web:fromCurrency>USD</web:fromCurrency>
<web:toCurrency>EUR</web:toCurrency>
<web:amount>50</web:amount>
</web:ConvertCurrency>
</soap:Body>
</soap:Envelope>
```
- **SOAP Response:**
```xml
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:web="http://www.example.com/webservice">
<soap:Header/>
<soap:Body>
<web:ConvertCurrencyResponse>
<web:result>42.5</web:result>
</web:ConvertCurrencyResponse>
</soap:Body>
</soap:Envelope>
```
4. **GraphQL API:**
- GraphQL is a query language for APIs that allows clients to request only the data they
need. Example using a blog API:
- **GraphQL Query:**
```graphql
query {
post(id: 123) {
title
author
comments {
text
```
- **Response:**
```json
{
"data": {
"post": {
"comments": [
```
These examples showcase different types of web APIs and the kinds of requests and
responses associated with each. The choice of API type depends on the specific requirements of the
application and the desired communication protocols.
WebAPI : api CALL IS LIKE one application is calling method/function over internet.
Web service : to have that depicted in machine processable format specified in web service
description Language (WSDL)
WEBAPI : third party vendors can write the program that interface with other programs
All Web Services are EWEB API BUT ALL web api are not web services.
The terms "Web API" and "Web service" are sometimes used interchangeably, but there are
distinctions between them. Let's clarify the differences:
1. **Definition:**
- **Web Service:** This is a broader term that refers to any service offered over the web.
It encompasses a variety of communication protocols and technologies, including APIs. Web services
can use SOAP (Simple Object Access Protocol), REST (Representational State Transfer), XML-RPC,
JSON-RPC, and other protocols for communication.
2. **Communication Protocols:**
- **Web Service:** Can use various communication protocols, including SOAP, REST, XML-
RPC, etc. It's a more general term that encompasses different technologies.
- **Web API:** Often associated with RESTful services that use standard HTTP methods
(GET, POST, PUT, DELETE). Web APIs can use other protocols as well, but RESTful APIs are a common
and popular implementation.
3. **Data Formats:**
- **Web Service:** Supports various data formats, including XML and JSON. SOAP-based
services often use XML for message formatting.
- **Web API:** Often associated with RESTful APIs that commonly use JSON as the
preferred data format due to its lightweight and easy-to-read nature.
4. **Usage and Context:**
- **Web Service:** The term "web service" is more generic and can refer to any service
provided over the web, regardless of the communication protocol or technology used.
- **Web API:** Typically used to refer to APIs that follow RESTful principles and use HTTP
methods for communication. It is more specific and often implies a lightweight, resource-oriented
approach.
5. **Architecture:**
- **Web API:** Often associated with RESTful architecture, which emphasizes a stateless,
resource-based approach.
What is RESTAPI :
Rest api is an api or web api that have an architectural style of REST .
1.client server
2.unifrom interface
3.layered system
4. cache
5.statelessness
6.code of demand
1. **Statelessness:** Each request from a client to a server must contain all the information needed
to understand and fulfill that request. The server should not store any information about the client's
state between requests. This enhances scalability and simplifies the server architecture.
2. **Resources:** Resources are key abstractions in RESTful APIs, and they are identified by unique
URIs (Uniform Resource Identifiers). Resources can represent entities such as users, products, or any
other concept relevant to the application.
3. **HTTP Methods:** RESTful APIs use standard HTTP methods to perform operations on
resources. Common methods include:
4. **Representation:** Resources can have different representations, such as JSON or XML. Clients
interact with resources by exchanging representations. For example, when retrieving a user
resource, the server might respond with a JSON representation of that user.
5. **Uniform Interface:** RESTful APIs provide a uniform and consistent way to interact with
resources. This includes a common set of conventions for resource identification, resource
representation, and resource manipulation.
6. **Stateless Communication:** Each request from a client to a server must contain all the
information needed to understand and fulfill that request. The server should not store any
information about the client's state between requests.
```
GET /api/tasks/123
```
In this example:
- `/api/tasks/123` is the URI that identifies the resource (in this case, a task with ID 123).
RESTful APIs are widely used in web development to enable communication between different
systems, allowing them to exchange data and perform actions over the web. They are the
foundation of many modern web and mobile applications.
SOA is a web architecture style of web service , but it is not a programming language or technology.
System is made up of services and those services are performs some operations and they can
communicate with each other
Ex : getting data from database , calling external system, applying some algorithum.
2. object oriented achitectire (OOA) :
Communicate with object instance and mainly it is for managing lifecycle of object.
Resources :
Resource is anything that can stored in computer and any document or data
So the idea of ROA is use to web technologies (http, url ,and xml/json)with the core design
principles and providing guidelines to support and implement interaction of resources.
Resources provider :
EX : AWS , azure
SOAP stands for Simple Object Access Protocol. It is a XML-based protocol for
accessing web services.
SOAP (Simple Object Access Protocol) is a protocol for exchanging structured information in web
services. It is a messaging protocol that allows programs running on different operating systems to
communicate with each other by using XML (eXtensible Markup Language) as the message format.
SOAP was developed to enable communication between applications over a network, often the
internet.
1. **XML-Based Messaging:**
- SOAP messages are typically formatted as XML documents. XML is used to structure the data
being sent in the message, providing a standardized way to represent information.
- SOAP allows programs written in different programming languages and running on different
platforms to communicate with each other. This is because XML is a platform-independent and
language-neutral data format.
3. **Protocol Neutrality:**
- SOAP can be used with various transport protocols, including HTTP, SMTP, and more. However, it
is most commonly associated with HTTP, making it suitable for web-based communication.
4. **Extensibility:**
- SOAP messages can include additional elements beyond the mandatory ones defined by the
standard. This extensibility allows for the inclusion of application-specific information and additional
features.
- WSDL is an XML-based language that is used to describe the functionality offered by a web
service. It provides a standardized way to define the methods, input parameters, and output
parameters of a web service. WSDL is often used in conjunction with SOAP.
- SOAP supports a remote procedure call (RPC) style of communication, where a client can invoke
methods or procedures on a remote server and receive the results.
A basic example of a SOAP message might look like this:
```xml
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope"
xmlns:example="http://www.example.com">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<example:SayHello>
<example:Name>John</example:Name>
</example:SayHello>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
```
In this example:
- The `<example:Name>` element contains a parameter (in this case, the name "John") passed to the
`SayHello` operation.
SOAP is used in various contexts, especially in enterprise-level applications and web services where a
standardized, platform-independent, and extensible messaging protocol is required. However, it's
worth noting that in more recent years, lightweight and less verbose alternatives like REST
(Representational State Transfer) have become popular for certain types of web services due to their
simplicity and ease of use.
RESTful Web Services
REST stands for REpresentational State Transfer.
Language and Platform independent: RESTful web services can be written in any
programming language and executed in any platform.
Can use SOAP: RESTful web services can use SOAP web services as the
implementation.
Permits different data format: RESTful web service permits different data format
such as Plain Text, HTML, XML and JSON.
2) SOAP stands for Simple Object REST stands for REpresentational State Transfer.
Access Protocol.
3) SOAP can't use REST because it is a REST can use SOAP web services because it is a concept
protocol. and can use any protocol like HTTP, SOAP.
4) SOAP uses services interfaces to REST uses URI to expose business logic.
expose the business logic.
5) JAX-WS is the java API for SOAP JAX-RS is the java API for RESTful web services.
web services.
6) SOAP defines standards to be REST does not define too much standards like SOAP.
strictly followed.
7) SOAP requires more REST requires less bandwidth and resource than SOAP.
bandwidth and resource than REST.
8) SOAP defines its own security. RESTful web services inherits security measures from
the underlying transport.
9) SOAP permits XML data format REST permits different data format such as Plain text,
only. HTML, XML, JSON etc.
10) SOAP is less preferred than REST. REST more preferred than SOAP.
There are many differences between SOAP and REST web services. The important 10
differences between SOAP and REST are given below:
One of the key features of a REST API is that it is stateless, which means that the
server does not store any information about the client's state. Instead, all information
required to service a request is included in the request itself. This allows for greater
scalability and flexibility, as the server does not need to maintain any state
information about the client.
Another key feature of a REST API is that it uses HTTP methods, such as GET, POST,
PUT and DELETE, to indicate the type of operation the client requests. For example, a
GET request retrieves information from the server, while a POST request is used to
submit information to the server. This allows for a simple and consistent way of
interacting with the server, regardless of the type of data being exchanged.
A REST API also uses a simple and consistent URL structure, which makes it easy for
developers to understand and interact with the API. The URL structure typically
includes the base URL, followed by a resource path and an optional query string. The
resource path is used to specify the specific resource that the client is interacting
with, while the query string is used to pass additional information to the server.
API stands for Application Programming Interface, and it's a set of protocols and tools for building
software applications. It defines the methods and data formats that applications can use to
communicate with each other. An API can be a set of rules allowing different software applications
to communicate with each other.
REST API (Representational State Transfer API) is a type of API that follows the principles of REST
architecture. REST is an architectural style that uses standard HTTP methods (GET, POST, PUT,
DELETE) for communication and is often used in web services development.
1. **Definition:** It is a set of protocols, routines, and tools for building software and
applications.
2. **Communication:** It can use various protocols for communication, including HTTP, SOAP,
RPC, etc.
4. **Data Formats:** Supports various data formats, including XML, JSON, etc.
5. **Usage:** Can be used in various types of applications, including web applications, mobile
applications, libraries, etc.
2. **Communication:** Uses standard HTTP methods (GET, POST, PUT, DELETE) for
communication.
5. **Usage:** Often used in web services development, where simplicity, scalability, and
statelessness are important.
**Example:**
- **REST API Example:** A web service that allows you to retrieve, create, update, or delete data
using standard HTTP methods.
In summary, REST API is a specific type of API that adheres to the principles of REST, which is an
architectural style. APIs, in general, can use various communication protocols and can be
implemented in different ways based on specific requirements.