REST/RESTful

Introduction

  • It is also called RESTful API.
  • REST stands for Representational State Transfer (REST).

Definition

  • RESTful API is a type of web service architecture that follows the principles of Representational State Transfer (REST).

Characteristic Features

  • REST is an architectural style for designing networked applications.
  • They are known for their simplicity, scalability, and ease of use, and the use of standard HTTP methods for communication making them a popular choice for modern web development.
  • Characteristics of RESTful web services:
    • Statelessness:
      • RESTful services are stateless, meaning that each request from a client to the server must contain all the information needed to understand and process the request. There should be no server-side session state that the server relies on between requests.
    • Resources and URLs:
      • In REST, resources are represented as entities, and each resource is identified by a unique URL (Uniform Resource Locator).
      • Resources can represent data objects, such as users, products, or articles, and the URLs are used to access and manipulate these resources.
    • HTTP Methods:
      • RESTful services use standard HTTP methods to perform actions on resources.
      • The most commonly used HTTP methods in REST are:
        1. GET: Retrieve data from the server (read).
        2. POST: Create a new resource on the server.
        3. PUT: Update an existing resource on the server.
        4. DELETE: Remove a resource from the server.
    • Uniform Interface:
      • A uniform and consistent interface is a fundamental principle of REST.
      • Clients interact with resources using standard HTTP methods, and the structure of the URLs and the data format (usually JSON or XML) should be consistent and predictable.
    • Representation:
      • Resources can have multiple representations (e.g., JSON, XML, HTML) depending on client preferences.
      • The client specifies the desired representation in the HTTP Accept.
    • Stateless Communication:
      • Each request from the client to the server should be self-contained and not rely on previous requests or server-side state.
      • Authentication and session information can be included in the request headers.
    • Layered System:
      • REST allows for the use of intermediaries, such as proxy servers, load balancers, and caches, without affecting the overall system architecture. This supports scalability and performance.
    • Response Codes:
      • HTTP response codes (e.g., 200 OK, 404 Not Found, 500 Internal Server Error) are used to indicate the result of a request. They provide information about the success or failure of the request.
    • HATEOAS (Hypermedia as the Engine of Application State):
      • HATEOAS is a constraint in REST that suggests including hyperlinks in responses, allowing clients to navigate the API dynamically by following links provided in the resource representations.
    • Security:
      • RESTful APIs can use various security mechanisms, such as HTTPS for data encryption, authentication, and authorization mechanisms to protect resources.

    Example

    An example of a RESTful API endpoint is:

      • URL: https://api.example.com/users/123
      • HTTP Method: GET
      • Action: Retrieve user information for the user with ID 123.

    Use/Application

    • RESTful web services are widely used for building web and mobile applications, and various other distributed systems as well as for providing APIs for external developers to access and interact with data and services. 

    JSON

    (Link for the difference between JSON and XML)

    Introduction

    • JSON stands for “JavaScript Object Notation”.

    Definition

    • JSON is a lightweight data interchange format that is easy for humans to read and write and easy for machines to parse and generate.

    Characteristics Features

    • JSON has become a popular alternative to XML for structuring data due to its simplicity and efficiency.
    • JSON is a lightweight format with minimal overhead, making it efficient for data transmission and storage.
    • JSON is a format for structuring or storing, and transporting data. These types of formats are generally preferred by web applications to share.
    • JSON is widely supported in programming languages, and many libraries and tools for parsing and generating JSON data.
    • JSON is designed to be easy for humans to read and write.
    • JSON uses a simple and intuitive syntax that resembles the syntax for JavaScript objects, making it familiar to developers.

    Structure

    • JSON represents structured data as key-value pairs. Here, each key is a string, and its corresponding value can be a string, number, boolean, object, array, or null depending upon requirements.
    • JSON objects are enclosed in curly braces {} and contain a collection of key-value pairs.
    • Arrays are ordered collections enclosed in square brackets [ ]. 
    • JSON supports various Data Types:-
      • Strings: Enclosed in double quotes, e.g., “name”: “Raman”.
      • Numbers: Integer or floating-point values without double quotes, e.g., “age”: 30.
      • Booleans: As true or false, e.g., “isStudent”: true.
      • Objects: Enclosed in curly braces, e.g., “address”: {“street”: “123 Main Road”, “city”: “Patna”}.
      • Arrays: Ordered collections of values, e.g., “colors”: [“red”, “green”, “blue”].
      • Null: Represents a null or empty value, e.g., “middleName”: null.
    • JSON allows the nesting of objects and arrays, allowing for creation complex data structures.
    • Syntax Rules:
      • Keys and strings must be enclosed in double quotes.
      • Keys are separated from values by a colon.
      • Key-value pairs are separated by commas.
      • Objects are enclosed in curly braces.
      • Arrays are enclosed in square brackets.

    Examples

    • A common example of a simple JSON object representing information about a person is:-
    {
      “name”: “Raman”,
      “age”: 30,
      “isStudent”: false,
                         “isPerson”: true,
      “address”: {
                            “street”: “123 Main Road”,
                            “city”: “Patna”
                        },
      “hobbies”: [“reading”, “moving”, “cooking”]
    }

    Uses/Applications

    • JSON is used extensively in web development for sending and receiving data between client and server, as well as in various other applications for data storage and configuration files.
    • JSON is often used for data exchange between a server and a web application, as well as for configuration files and data storage.
    Categories: Android

    0 Comments

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.