#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
HTTP

Response Code in HTTP

An HTTP response code, or status code, is the three-digit number a server sends at the start of every response to say how the request went. Your code reads it to decide what to do next: show the data, ask the user to log in, display a "not found" message, or try again later.

The five families

The first digit tells you the category, so even an unfamiliar code is easy to interpret.

RangeMeaningTypical examples
1xxInformational, the request is in progress101 Switching Protocols
2xxSuccess200 OK, 201 Created, 204 No Content
3xxRedirection, look somewhere else301 Moved Permanently, 304 Not Modified
4xxClient error, the request had a problem400, 401, 403, 404, 422, 429
5xxServer error, the server failed500, 502, 503

The codes you will handle most

  • 200 OK: the standard success, with data in the body.
  • 201 Created: a POST created a new resource. The body often contains the new item.
  • 204 No Content: success with an empty body, common after a DELETE. Calling res.json() on it will throw.
  • 400 Bad Request: the data sent was malformed or failed validation.
  • 401 Unauthorized: the user is not authenticated. Despite the name, it means "we do not know who you are".
  • 403 Forbidden: the server knows who you are, but you are not allowed to do this.
  • 404 Not Found: no resource exists at that path.
  • 429 Too Many Requests: you hit a rate limit; wait before retrying.
  • 500 Internal Server Error: a bug or crash on the server. The client usually cannot fix it.

fetch does not reject on error codes

This is the single biggest trap for front-end developers. The fetch promise rejects only when the request could not be completed at all, such as no network connection. A 404 or 500 is still a completed response, so fetch resolves normally. You must check response.ok (true for 200 to 299) or response.status yourself.

async function loadCourse(id) {
  const res = await fetch(`/api/courses/${id}`);

  if (res.status === 404) {
    return null;                       // show a "not found" message
  }
  if (res.status === 401) {
    throw new Error('Please log in again.');
  }
  if (!res.ok) {
    throw new Error(`Server error: ${res.status}`);
  }
  return res.json();
}

Libraries such as Axios behave differently and reject for any status outside the 2xx range, so read the documentation of whatever client you use.

Choosing the right code on the server

If you write the API yourself, in Express for example, pick codes that match what happened. A classic bad practice is returning 200 with a body like {"error": "Not found"}. It forces every client to inspect the body, breaks caching and monitoring, and hides failures. Send res.status(404).json({ message: 'Course not found' }) instead.

Also avoid leaking details in 500 responses. Log the stack trace on the server and send the client only a short, general message.

Reading codes in practice

The browser's Network tab colours failed requests red and shows the code for each one. When something breaks, check the status first: a 4xx points at what the client sent, a 5xx points at the server.

To sum up: read the first digit for the category, check res.ok after every fetch, and return honest, specific codes from your own APIs.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course