Summary of Data Exchange and Web APIs
Data Exchange & Web APIs: A Student's Guide to Digital Communication
Introduction
A Web API (Application Programming Interface) enables communication between a client and a server over a network. It defines a set of rules and interfaces that applications use to exchange data — typically over HTTP(S). This material summarizes the principles of HTTP, REST, alternative architectures (JSON:API, GraphQL), describing APIs using OpenAPI, and related technologies such as AJAX or WebSocket.
Definition: A Web API is a set of specified methods, protocols, and formats that enable client applications to securely and structuredly call functions and share data with a server.
Communication Fundamentals: HTTP and Client-Server Architecture
Client and Server
- Client: A web browser, mobile app, bot, IoT device, or any other application that initiates a request.
- Server: A web or application server (Apache, Nginx, IIS), typically running on a physical or virtual machine.
HTTP Principles
- HTTP is a protocol layered on top of TCP/QUIC, operating at the application layer. It's a request-response protocol, stateless (state is not automatically maintained between requests).
- The standard port is 80; for secure HTTPS traffic, we use TLS (port 443).
Definition: Stateless means that the server does not retain information about previous client requests; each request contains everything needed for processing.
HTTP Methods (Most Important)
- GET – Reads a resource (without side effects).
- POST – Sends data to the server (e.g., creating a resource or submitting a form).
- PUT – Creates or replaces a representation of the target resource.
- PATCH – Partially updates a resource.
- DELETE – Deletes a resource.
Others: HEAD, OPTIONS, CONNECT, TRACE (primarily for diagnostics or specific scenarios).
HTTP Headers and Response Codes
- Headers carry metadata (User-Agent, Content-Type, Authorization, Cache-Control, etc.).
- Response code groups:
- 1xx informational
- 2xx success (200, 201, 204)
- 3xx redirection (301, 302, 304)
- 4xx client error (400, 401, 403, 404, 405)
- 5xx server error (500, 502, 503)
REST (REpresentational State Transfer)
Core Principles
- REST is an architectural style that uses HTTP as its transport layer.
- Everything is a resource identified by a URI; clients interact with it using a uniform interface (HTTP methods).
- Key constraints: stateless, cacheability, uniform interface, layered system.
Definition: A REST API provides access to resources over HTTP, where resources are identified by URIs and operations are performed using standard HTTP methods.
Data Formats
- Most commonly, JSON is used; XML was common previously. REST does not restrict the format, but JSON is more modern for the web.
Example REST URLs and Operations
- GET https://api.example.com/users/123 — retrieve user with ID 123
- POST https://api.example.com/users — create a new user
- PUT https://api.example.com/users/123 — replace user 123
- PATCH https://api.example.com/users/123 — modify parts of user 123
- DELETE https://api.example.com/users/123 — delete user 123
OpenAPI Specification (OAS)
- OAS is an API description in JSON or YAML format that allows for generating documentation, client stubs, mock servers, and tests.
- Main sections: openapi, info, servers, paths (operations), components (schemas, parameters, securitySchemes).
JSON:API and GraphQL — Alternatives to "Pure" REST
JSON:API
- A specification that defines a consistent JSON format for REST APIs (media type application/vnd.api+json).
- Standardizes the structure of responses, including relationships, meta, and included sections.
Already have an account? Sign in
Web API - Overview
Klíčové pojmy: A Web API defines an interface for communication between applications over a network., HTTP is the foundational protocol for Web APIs; it uses methods such as GET, POST, PUT, PATCH, and DELETE., REST utilizes resources identified by URIs and standard HTTP methods., OpenAPI (OAS) describes APIs in JSON/YAML format, enabling the generation of documentation and stubs., JSON is the primary data format for modern REST APIs; XML is typically used in older services., GraphQL allows clients to specify exactly the data they need, reducing the number of requests., WebSocket provides a persistent, bidirectional connection for real-time communication., AJAX (Fetch/XHR) enables asynchronous updates without a page reload., SOAP and XML-RPC are used in enterprise scenarios with rigidly defined contracts., Security: always use HTTPS/TLS, employ OAuth/JWT, and implement rate limiting., HTTP responses include status codes (1xx–5xx) that describe the outcome of a request., Utilize proper documentation and validation (e.g., OAS, schemas) for robust APIs.