RESTful API Overview

RESTful API

What is RESTful APIs?

  • Definition: API (Application Programming Interface) is a set of rules enabling communication between programs.

  • Creation: Developed by server-side developers, allowing clients to interface with the server.

  • REST: Stands for Representational State Transfer, a framework guiding how APIs should be structured.

  • Requests and Responses:

    • Data can be fetched via specific URLs (requests).

    • Each URL corresponds to a response.

Traditional HTML Model

  • Components:

    • Client (Browser): Sends request to server.

    • Server: Processes request and sends back response.

Mass Server Model

  • Components:

    • Various clients (Mobile App, Code, SPA) communicating with the server:

    • Client (Mobile App)

    • Client (Code)

    • Client (Single Page Application)

    • Server (for users and data)

Action and Resource Based URL

  • Action Based URLs:

    • Centered on the action performed.

    • Usually include a verb in the URL.

    • Example: GET /returnItem/item?id=123

  • Resource Based URLs:

    • Focus on the resource itself.

    • Typically consist of nouns.

    • Uses HTTP verbs (GET, PUT, POST, DELETE) to define action.

    • Example: GET /item/123/

Why Representation?

  • State Management:

    • Partial representation of resource state transferred between client and server.

    • States typically sent as JSON or XML.

Recommendations and Constraints

  • Key Principles of RESTful APIs:

    • Uniform interface (HTTP)

    • Resource identification in requests

    • Client-server architecture: Separation of concerns

    • Stateless nature: No client-context stored on server

    • Layered systems using intermediate servers

Ilities from REST

  • Long-lasting Web Service Attributes:

    • Scalability

    • Simplicity

    • Modifiability

    • Visibility

    • Portability

    • Reliability

The Request

  • Components of a Request: Consists of four parts:

    • Endpoint: The URL being requested.

    • Method: The type of request (GET, POST, etc.).

    • Headers: Additional information exchanged.

    • Data/Body: Information sent to the server.

Endpoint

  • Definition: The URL where requests are directed.

  • Path Configuration: Paths are defined in API documentation.

    • Use of symbols like : or {} to indicate variables.

  • Query Parameters:

    • Not part of REST standard but often included, starting with ? and separated by &.

    • Example: GET /users/:username/repos

Method

  • Request Types: Serve different purposes:

    • GET: Retrieve data.

    • POST: Create a new resource.

    • PUT/PATCH: Update existing resource.

    • DELETE: Remove a resource.

Calling CRUD Methods

  • GET (Read):

    • Request: Fetch data.

    • Response: Returns the requested data.

  • POST (Create):

    • Request: Adds a new entry in the database.

    • Response: Confirmation of creation.

  • PUT/PATCH (Update):

    • Request: Modifies existing database entry.

    • Response: Status of the update.

  • DELETE (Delete):

    • Request: Removes an entry from the database.

    • Response: Confirmation of deletion.

Header

  • Definition: Used to communicate additional information to the client/server.

  • Uses:

    • Authentication, type of content being sent, etc.

    • Format: HTTP headers in property-value pairs.

Data/Body

  • Definition: Contains the payload sent to the server.

  • Example Usage:

    • Command: curl -X POST <URL> -d name=value.

Known Challenges for REST

  • Challenges Include:

    • Endpoint consensus and declaration consistency.

    • API versioning and full system updates.

    • Handling security concerns including DDoS attacks.

    • Managing multiple requests and unnecessary data transfers.

Summary

  • Key Takeaways:

    • Understanding of APIs and RESTful services.

    • Importance of action vs resource-based URLs.

    • Basics of making a RESTful request and executing it.