Lección 3

fetch vs Axios Output en español

Guía en español para curl converter fetch vs axios output: Know what changes when a request moves from terminal cURL to browser or Node code.

Este contenido todavía no está disponible en español. Se muestra la versión en English mientras completamos la localización.

Converting cURL to fetch or Axios is not only a syntax change. Runtime behavior can change too.

fetch

fetch is available in modern browsers and modern Node runtimes. It returns a response object and does not throw for HTTP 4xx or 5xx statuses.

const response = await fetch("https://api.example.com/users", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ name: "Ada" })
});

if (!response.ok) {
  throw new Error(`HTTP ${response.status}`);
}

Axios

Axios wraps request and response behavior differently. It typically rejects for non-2xx responses and parses JSON automatically when possible.

const response = await axios.post(
  "https://api.example.com/users",
  { name: "Ada" },
  { headers: { "Content-Type": "application/json" } }
);

Browser differences

A cURL command can send almost any header from your machine. Browser code is limited by CORS, forbidden headers, cookies, and credential rules. A converted request may be valid JavaScript but still fail in the browser.

Node differences

Node code may need environment variables, proxy settings, TLS options, or timeout handling that a simple cURL example does not show.

Treat generated code as a starting point. Review runtime assumptions before pasting it into production code.

Volver al resumen del curso