Sobes.tech
Junior — Middle

Kuidas määrata, kas API vastab RESTful arhitektuuri põhimõtetele?

sobes.tech AI

Vastus AI-lt

A RESTful API complies with REST principles if the following key criteria are met:

  • Proper use of HTTP methods: GET for retrieving data, POST for creating, PUT/PATCH for updating, DELETE for deleting.
  • Resource identification via URI: each resource has a unique and understandable URL.
  • Statelessness on the server: each request contains all necessary information, the server does not store client state.
  • Use of standard HTTP response codes: e.g., 200 OK, 404 Not Found, 201 Created.
  • Support for various representation formats (e.g., JSON, XML) through Accept/Content-Type headers.

This can be verified by analyzing the API for compliance with these principles and correct use of HTTP.


Example:

GET /users/123 HTTP/1.1
Host: example.com
Accept: application/json

HTTP/1.1 200 OK
Content-Type: application/json

{"id":123, "name":"Ivan"}