Volver a APIs

API Posts

Gestión de usuarios, posts y comentarios con autenticación JWT.

Base URL: /api/v2

Autenticación

Para usar endpoints protegidos, primero regístrate o inicia sesión para obtener un token.

Registro

Login

No autenticado
GET /api/v2/posts
Obtener todos los posts (público)
// Ejemplo básico fetch('/api/v2/posts') .then(res => res.json()) .then(data => console.log(data));
POST /api/v2/posts
Crear nuevo post (requiere autenticación)
// Requiere token en el header Authorization: Bearer {token} fetch('/api/v2/posts', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer {token}' }, body: JSON.stringify({ titulo: "Mi post de prueba", contenido: "Contenido del post...", categoria: "programacion", etiquetas: "api,php,test" }) })
PUT /api/v2/posts/{id}
Actualizar un post existente (requiere autenticación)
fetch('/api/v2/posts/1', { method: 'PUT', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer {token}' }, body: JSON.stringify({ titulo: "Nuevo título", contenido: "Nuevo contenido" }) })
DELETE /api/v2/posts/{id}
Eliminar un post (requiere autenticación)
fetch('/api/v2/posts/1', { method: 'DELETE', headers: { 'Authorization': 'Bearer {token}' } })
POST /api/v2/comentarios
Agregar comentario a un post (requiere autenticación)
fetch('/api/v2/comentarios', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer {token}' }, body: JSON.stringify({ post_id: 1, comentario: "Excelente post, muy útil!" }) })
DELETE /api/v2/comentarios/{id}
Eliminar un comentario (requiere autenticación)
fetch('/api/v2/comentarios/1', { method: 'DELETE', headers: { 'Authorization': 'Bearer {token}' } })