Fetch and Parse a JSON API

Easy ⏱ 8 min 77% acceptance ★★★★★ 4.5
Write fetch_user(base_url, user_id) using requests.get on f'{base_url}/users/{user_id}', calling raise_for_status() so HTTP errors raise, and returning response.json() — the canonical three-line GET.

Examples

Example 1
Input
fetch_user('https://api.example.com', 7)
Output
the parsed JSON dict for user 7
Explanation

get → raise_for_status → json.

Constraints

  • Call raise_for_status().
  • Return the parsed body.

Topics

APIsrequests.get

Companies

TCSInfosysPostman

Hints

Hint 1

requests.get(url) returns a Response.

Hint 2

.json() parses the body for you.

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