Este memo fue escrito para necesidades internas (para abrir los ojos de colegas menos experimentados en la web). Pero porque He visto suficientes bicicletas de oficinas bastante respetadas, al parecer, - lo extendĂ en el centro Me parece que muchos serĂĄn Ăștiles.
Porque
Espero que el lector ya entienda por quĂ© realmente necesita la API REST, y no algĂșn tipo de monstruo como SOAP. La pregunta es, Âżpor quĂ© seguir algunos estĂĄndares y prĂĄcticas si los navegadores parecen permitirle hacer lo que quiere?
- El eståndar HTTP es el eståndar. Su incumplimiento es perjudicial para el karma y genera problemas constantes con la seguridad, el almacenamiento en caché y otros navegadores, que no son en absoluto, sino que simplemente siguen el eståndar.
{error: "message","result":...}
- . , api , .
- .
200
â , .
http-
METHOD URI
METHOD â (GET, PUT ..), URI â .
â key: value
, , â , .
http, ( HTTP/1.1 200 OK
), , , .
.
, â UTF-8 UTF-8, .. , , "" charset.
html- , .. JSON_UNESCAPED_UNICODE . html ( - ù ), html. / \uXXXX; &#XX;. "" .
, URI , JSON. , javascript JSON.
, . json-, "" javascript, .
Accept: application/json, */*; q=0.01
API (, html URI) application/json Accept.
Accept / , , text/javascript
, , "application/json".
2
Content-Type: application/json; charset=UTF-8
Content-Type: application/json; charset=UTF-8
, ,
Content-Type: multipart/form-data
,
-----------------
Content-Type: application/json; charset=UTF-8
Content-Disposition: form-data; name="data"
-----------------
Content-Type: image/jpeg
Content-Disposition: form-data; name="avatar"; filename="user.jpg"
CSRF ( ), CSRF- ( X-CSRF-Token) , . CSRF , , CSRF .
URI
, â URI
/:entity[/:id][/?:params]
, api - ,
/api/:entity[/:id][/?:params]
:
- entity â , , / . :
users
, dictionary
- id opt. â . , . :
/users/10
, /dictionary/ru/apptitle
- params opt. â (, , .). HTTP GET ( encodeURIComponent .)
URI
#^/(<entity>([a-z]\-_)+)/?(<id>([a-z][A-Z][0-9]\-_/)*)?$#
, .. , URL .
HTTP
GET /:entity/:id
â getById
200 OK
JSON ( - )
, id , 404 Not Found
, , .. GET HEAD . - :
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
GET /:entity[?param1=...¶m2=...]
â get
: 200 OK
JSON (.. [
]
).
, 200 OK
[]
.
: , â . â , , . api.
HEAD /:entity[/:id]
â
GET URI, , HTTP-.
HEAD - .
pre-flight , , . , Chrome head- CORS - ( ). head- , .
(, -).
POST /:entity
â :entity
JSON , .. {"field1":"value","field2":10}
201 Created
,
Location: /:entity/:new_id
.
, , id Location.
POST (RPC), 200 OK
. REST RPC api â , .
, .. POST .
PUT /:entity/:id
â
JSON.
204 No Data
, .. .
, .. PUT - .
PATCH /:entity/:id
â
, .
200 OK
, getById, .
, .. PATCH .
.
DELETE /:entity/:id
â , .
204 No Data
, .. .
, .. DELETE 404.
OPTIONS /:entity[/:id]
, URI.
200 OK
Allow: GET, POST, ...
.
4 ( ) 5 ( ). , , text/plain ( JSON). ,
Content-Type: text/plain; charset=UTF-8
html api â , .. , .
, . . , 401 HTTP-, - react electron.
UPD . : , , (CI), . , - (.. - ) , , - - . use-case, 404 410, . 404 200 500 â .
400 Bad Request
, .
403 Forbidden
, . , . . 419
404 Not Found
, entity id .
get entity (. ).
, 418.
, . , JSON , JSON.
418 I'm a Teapot
, . , URI, .
, URI (.. ) 404, ( ). 404, 418 . â " " 410 Gone
, , .. , - . 404 .
419 Authentication Timeout
, (, CSRF ). , , .
422 Unprocessable Entity
, .
, , , - .
, .
500 Internal Server Error
, .
, â console.error(err)
(, ).
501 Not Implemented
, ( ) .
, -, . !