{
    "componentChunkName": "component---src-pages-blog-markdown-remark-fields-slug-js",
    "path": "/blog/http-error-codes-401-vs-403",
    "result": {"data":{"markdownRemark":{"html":"<h2 id=\"table-of-contents\" style=\"position:relative;\"><a href=\"#table-of-contents\" aria-label=\"table of contents permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Table of contents</h2>\n<ul>\n<li><a href=\"#introduction\">Introduction</a></li>\n<li><a href=\"#http-401-unauthorized\">HTTP 401 UNAUTHORIZED</a></li>\n<li><a href=\"#http-403-forbidden\">HTTP 403 FORBIDDEN</a></li>\n<li><a href=\"#differences-between-error-codes-401-and-403\">Differences between error codes 401 and 403</a></li>\n<li><a href=\"#conclusion\">Conclusion</a></li>\n</ul>\n<h2 id=\"introduction\" style=\"position:relative;\"><a href=\"#introduction\" aria-label=\"introduction permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Introduction</h2>\n<p>Understanding HTTP status codes is imperative since most modern Web APIs leverage this protocol and on the surface, HTTP status codes are straightforward. A <code class=\"language-text\">200</code> response signifies the request was successfully completed while a <code class=\"language-text\">404</code>  signifies that the address was not found and a <code class=\"language-text\">500</code> means an internal error occurred in the backend server. But there seems to be some confusion between error codes <code class=\"language-text\">401 Unauthorized</code> and <code class=\"language-text\">403 Forbidden</code>. In this blog post, we will explore the distinctions between the two error codes and provide insights into the specific scenarios where each error code is applicable.</p>\n<h2 id=\"http-401-unauthorized\" style=\"position:relative;\"><a href=\"#http-401-unauthorized\" aria-label=\"http 401 unauthorized permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>HTTP 401 UNAUTHORIZED</h2>\n<p>The HTTP status code 401, often denoted as <code class=\"language-text\">UNAUTHORIZED</code>, signifies that the client lacks proper authentication credentials or has provided invalid credentials. In simpler terms, the server has failed to identify the user.</p>\n<p>This could occur due to:</p>\n<ul>\n<li>\n<p>Missing or Incorrect Credentials: The user hasn’t provided any credentials or the ones provided are incorrect. An example of this is the user trying to sign in with an incorrect password.</p>\n</li>\n<li>\n<p>Expired Credentials: If the user’s authentication token or session has expired, they won’t be granted access until they reauthenticate. For example in the context of the OAuth flow, this would mean that the access token is missing/revoked/expired.</p>\n</li>\n</ul>\n<h2 id=\"http-403-forbidden\" style=\"position:relative;\"><a href=\"#http-403-forbidden\" aria-label=\"http 403 forbidden permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>HTTP 403 FORBIDDEN</h2>\n<p>HTTP status code <code class=\"language-text\">403</code> also denoted as <code class=\"language-text\">FORBIDDEN</code> is returned when the server has successfully authenticated the user, but the user is still denied access to the requested resource. This is different from a 401 error, as the user’s credentials are valid, but they lack the necessary permissions to view or interact with the specific resource\nCommon scenarios that lead to 403 errors include:</p>\n<ul>\n<li>Insufficient permissions: The user’s credentials may not grant them the required privileges to access the resource. This could be due to their user role or other access restrictions.</li>\n<li>IP Blacklisting: Some websites restrict access based on IP addresses, preventing certain users or locations from accessing specific content.</li>\n</ul>\n<h2 id=\"differences-between-error-codes-401-and-403\" style=\"position:relative;\"><a href=\"#differences-between-error-codes-401-and-403\" aria-label=\"differences between error codes 401 and 403 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Differences between error codes 401 and 403</h2>\n<p>While both HTTP error codes indicate access denial, their fundamental differences lie in the context of authentication and authorization:</p>\n<ul>\n<li>\n<p>Authentication vs. Authorization: A 401 error focuses on invalid or missing authentication credentials, whereas a 403 error pertains to valid authentication but inadequate authorization.</p>\n</li>\n<li>\n<p>Credentials: A 401 error prompts the user to provide valid credentials, while a 403 error implies that the user’s credentials are valid but they lack the necessary permissions.</p>\n</li>\n<li>\n<p>Response to the User: A 401 error urges the user to log in or provide valid credentials. In contrast, a 403 error informs the user that access is forbidden, regardless of their authentication status.</p>\n</li>\n</ul>\n<h2 id=\"conclusion\" style=\"position:relative;\"><a href=\"#conclusion\" aria-label=\"conclusion permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Conclusion</h2>\n<p>To summarize the main difference between the two, although both status codes represent access denial, 401 errors address authentication issues, and 403 errors point towards authorization problems.\nIt is important to make this distinction as incorrectly handling these responses can leave you susceptible to exploits from malicious attackers.</p>\n<p>Getting these responses right in practice comes down to how you handle authentication and authorization. A dedicated auth solution like <a href=\"https://supertokens.com\" target=\"_blank\" rel=\"nofollow\">SuperTokens</a> manages <a href=\"https://supertokens.com/blog/session-based-authentication\" target=\"_blank\" rel=\"nofollow\">user sessions</a> and role-based access control for you, so your app returns the correct 401 vs. 403 response consistently instead of leaving it to ad-hoc checks.</p>\n<h2 id=\"related-reading\" style=\"position:relative;\"><a href=\"#related-reading\" aria-label=\"related reading permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Related reading</h2>\n<ul>\n<li><a href=\"https://supertokens.com/blog/what-is-502-bad-gateway-error\" target=\"_blank\" rel=\"nofollow\">What Is a 502 Bad Gateway Error and How to Fix It</a></li>\n<li><a href=\"https://supertokens.com/blog/cors-errors\" target=\"_blank\" rel=\"nofollow\">How to Fix CORS Errors</a></li>\n<li><a href=\"https://supertokens.com/blog/session-based-authentication\" target=\"_blank\" rel=\"nofollow\">Session-Based Authentication: How It Works</a></li>\n</ul>","frontmatter":{"date":"September 01, 2023","title":"Demystifying HTTP Error Codes: 401 vs 403 (2026)","cover":"http-error-codes-401-vs-403.png","author":"Joel Coutinho","description":"Learn the difference between HTTP 401 Unauthorized and 403 Forbidden errors, when each applies, and how to handle them correctly in your app."},"fields":{"slug":"/http-error-codes-401-vs-403/"}},"site":{"siteMetadata":{"title":"SuperTokens Blog"}}},"pageContext":{"id":"bff1b1b2-3da9-5a08-8ef1-7800393ec9ae","fields__slug":"/http-error-codes-401-vs-403/","__params":{"fields__slug":"http-error-codes-401-vs-403"}}},
    "staticQueryHashes": []}