Sobes.tech
Middle

Tell about your experience with REST API. List HTTP methods and response codes.

sobes.tech AI

Answer from AI

REST API is an architectural style for interaction between client and server via HTTP.

Main HTTP methods:

  • GET — retrieve data from the server.
  • POST — create a new resource.
  • PUT — update an existing resource (replace).
  • PATCH — partially update a resource.
  • DELETE — delete a resource.
  • OPTIONS — get supported methods from the server.

Main response codes:

  • 200 OK — request successfully completed.
  • 201 Created — resource successfully created (usually after POST).
  • 204 No Content — request successful, but no response body (e.g., after DELETE).
  • 400 Bad Request — client error, invalid request.
  • 401 Unauthorized — authentication required.
  • 403 Forbidden — access denied.
  • 404 Not Found — resource not found.
  • 500 Internal Server Error — server error.

Experience with REST API includes:

  • Formulating requests with correct methods and headers.
  • Handling responses and errors.
  • Authentication (e.g., via tokens).
  • Working with pagination, filtering, and sorting data.

Example of a request to get a list of users:

fetch('https://api.example.com/users', {
  method: 'GET',
  headers: {
    'Authorization': 'Bearer token123',
    'Accept': 'application/json'
  }
})
.then(response => {
  if (!response.ok) throw new Error(response.status);
  return response.json();
})
.then(data => console.log(data))
.catch(error => console.error('Error:', error));