Skip to content

401 Unauthorized

The request needs authentication. You are not logged in, or the credentials or token you sent are missing, invalid or expired.

What 401 Unauthorized means

Despite the name, 401 is about authentication: who you are. The server must send a WWW-Authenticate header saying how to authenticate, such as Basic or Bearer.

Compare with 403. A 401 means log in and try again; a 403 means the server knows who you are and still says no. Proxies use the separate 407 code.

Common causes

  • No Authorization header or session cookie was sent.
  • An expired or revoked access token, or a token issued for a different environment.
  • A wrong username or password for HTTP Basic auth.
  • Server clock skew making a signed token look expired or not yet valid.

How to fix 401 Unauthorized

  1. Log in again, or sign out and back in to get a fresh session.
  2. Check the header format, for example Authorization: Bearer <token>, with no extra quotes or spaces.
  3. Refresh the access token when it expires, and confirm it was issued for this API.
  4. Read the WWW-Authenticate header and the response body for the exact reason, such as invalid_token.

Headers that go with it

  • WWW-Authenticate

Example response

HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer realm="api"
Content-Type: text/plain; charset=utf-8

401 Unauthorized

How to check for a 401 response

cURL

# Print only the status code
curl -s -o /dev/null -w "%{http_code}\n" https://example.com/

JavaScript (Node.js)

const res = await fetch("https://example.com/");
if (res.status === 401) {
  console.log(res.status, res.statusText);
}

Python

import requests

res = requests.get("https://example.com/")
if res.status_code == 401:
    print(res.status_code, res.reason)

Go

res, err := http.Get("https://example.com/")
if err != nil {
	log.Fatal(err)
}
defer res.Body.Close()
if res.StatusCode == 401 {
	fmt.Println(res.Status)
}

Specification

401 Unauthorized is defined in RFC 9110, section 15.5.2.

  • 403

    Forbidden

    The server understood the request but refuses to allow it. Logging in again will not help unless your account gets access.

  • 407

    Proxy Authentication Required

    A proxy between you and the website needs you to authenticate before it will forward your request.

  • 511

    Network Authentication Required

    You need to log in to the network before you can get online, typically on hotel, airport or cafe Wi-Fi with a captive portal.

← all 63 HTTP status codes

Type to search posts, tools and projects.

Keyboard shortcuts

Search+Kor/
Go to homegthenh
Go to bloggthenb
Go to projectsgthenp
Go to timelinegthent
Show this help?
Close search or helpesc