# APIv2
- Básicamente hacer lo que dice [acá](https://juanda.gitbooks.io/webapps/content/api/arquitectura-api-rest.html)
- Estructura de la request
- Estructura de la response
- Códigos de estado de respuesta HTTP
- Retornar los recursos asociados (flags) o links a esos recursos
- Manejo de errores, estructura de respuestas y códigos correctos
## Estructura de Request
``` json
{
"attribute_1": "...",
"attribute_2": "..."
}
```
## Estructura de Response
#### Secciones
- data (records)
- id
- type
- attributes
- relationships
- data
- links (?)
- included (extra records coming from relationships)
- id
- type
- attributes
- relationships
- data
- links (?)
- meta
- extra information releated to pagination, records, etc.
:::spoiler Click to show an example
``` json
{
"data": [{
"type": "articles",
"id": "1",
"attributes": {
"title": "JSON:API paints my bikeshed!"
},
"relationships": {
"author": {
"links": {
"self": "http://example.com/articles/1/relationships/author",
"related": "http://example.com/articles/1/author"
},
"data": { "type": "people", "id": "9" }
},
"comments": {
"links": {
"self": "http://example.com/articles/1/relationships/comments",
"related": "http://example.com/articles/1/comments"
},
"data": [
{ "type": "comments", "id": "5" },
{ "type": "comments", "id": "12" }
]
}
},
"links": {
"self": "http://example.com/articles/1"
}
}],
"included": [{
"type": "people",
"id": "9",
"attributes": {
"firstName": "Dan",
"lastName": "Gebhardt",
"twitter": "dgeb"
},
"links": {
"self": "http://example.com/people/9"
}
}, {
"type": "comments",
"id": "5",
"attributes": {
"body": "First!"
},
"relationships": {
"author": {
"data": { "type": "people", "id": "2" }
}
},
"links": {
"self": "http://example.com/comments/5"
}
}, {
"type": "comments",
"id": "12",
"attributes": {
"body": "I like XML better"
},
"relationships": {
"author": {
"data": { "type": "people", "id": "9" }
}
},
"links": {
"self": "http://example.com/comments/12"
}
}],
"meta": {
"total": 12,
"lang": "es",
"country": "CL"
}
}
```
:::
## Códigos de estado de respuesta HTTP
:::info
### Respuestas informativas :mega:
- **100 Continue** – Esta respuesta provisional indica que todo hasta ahora está bien y que el cliente debe continuar con la solicitud o ignorarla si ya está terminada.
- **102 Processing** – El servidor ha recibido la solicitud y aún se encuentra procesandola, por lo que no hay respuesta disponible.
:::
:::success
### Respuestas satisfactorias :100:
- **200 OK** – Request exitosa (en el body debería estar el recurso esperado).
- **201 Created** – Recurso creado.
- **202 Accepted** – La solicitud se ha recibido, pero aún no se ha actuado. Es una petición "sin compromiso".
- **204 No content** - Request exitosa sin body.
- **206 Partial Content** - Cuando queremos devolver parte del recurso solicitado mientras el proceso continua.
:::
:::warning
### Redirecciones :zap:
:::
:::danger
### Errores de los clientes :face_palm:
- **400 Bad Request** – No pasa alguna validación.
- **401 Unauthorized** – Usuario no autenticado.
- **403 Forbidden** – Usuario autenticado pero sin permisos para acceder a un recurso.
- **404 Not Found** – El recurso al que se intenta acceder no existe.
:::
:::danger
### Errores del servidor :fire:
- **500 Internal server error** – Error genérico que debería evitarse lo máximo posible.
- **502 Bad Gateway** – Error en el servidor al intentar conectarse a un servicio ajeno a él.
- **503 Service Unavailable** – Error grave e inesperado del servidor.
:::