Call a Protected Endpoint

Medium ⏱ 12 min 48% acceptance ★★★★★ 4.9
Write fetch_private(url, token) sending the standard Authorization: Bearer <token> header, returning the parsed body on 200 but the string 'unauthorized' (without raising) when the status is 401 — checking response.status_code explicitly.

Examples

Example 1
Input
fetch_private(url, 'expired-token')
Output
'unauthorized' when the API answers 401
Explanation

The 401 branch is handled, not raised.

Constraints

  • Header name/format exactly as shown.
  • Only 401 maps to 'unauthorized'; other errors may raise.

Topics

APIsauth headers

Companies

OktaAuth0GitHub

Hints

Hint 1

headers={'Authorization': f'Bearer {token}'}.

Hint 2

Check status_code before raise_for_status.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.