HTTP and APIs: how programs talk to each other over the internet

Requests and responses, GET/POST/PUT/DELETE methods, status codes and JSON, with an interactive request builder.

When your phone checks the weather, it sends a REQUEST to a server and gets a RESPONSE back. That dialogue uses the HTTP protocol. An API is simply the list of requests a server understands.

Request and response

Una petición HTTP tiene: un método (qué quieres hacer), una URL (sobre qué), unas cabeceras (metadatos) y a veces un cuerpo (datos). La respuesta tiene: un código de estado (cómo fue), cabeceras y normalmente un cuerpo (los datos que pediste, en JSON).

Request builder

Monta una petición contra una API de ejemplo con /usuarios y mira la respuesta:

The methods

Status codes

Consuming an API from code

// JavaScript (fetch)
const res = await fetch("https://api.ejemplo.com/usuarios/1");
if (!res.ok) throw new Error("HTTP " + res.status);   // 404, 500...
const usuario = await res.json();
console.log(usuario.nombre);

// POST con cuerpo
await fetch("https://api.ejemplo.com/usuarios", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ nombre: "Marta" })
});

The same in JavaScript asíncrono, Flutter and Java.

Check what you've learned