📖 Scenario: Imagine you want to build a simple app that shows weather information from different cities. To get this data, your app needs to talk to a server that stores weather details. REST APIs help your app and the server communicate smoothly.
🎯 Goal: Learn why REST APIs exist by creating a simple example that shows how a client can request data from a server using REST principles.
📋 What You'll Learn
Create a simple data structure to hold weather information
Set up a configuration variable for the city to look up
Write a function that simulates a REST API call to get weather data for the city
Print the weather information for the chosen city
💡 Why This Matters
🌍 Real World
REST APIs are used everywhere to let apps and websites talk to servers and get data like weather, news, or user info.
💼 Career
Understanding REST APIs is key for software developers, web developers, and anyone working with modern apps that need to share data.
Progress0 / 4 steps
1
Create the weather data dictionary
Create a dictionary called weather_data with these exact entries: 'New York': 'Sunny', 'London': 'Cloudy', 'Tokyo': 'Rainy'
Rest API
Hint
Use curly braces to create a dictionary with city names as keys and weather descriptions as values.
2
Set the city to look up
Create a variable called city and set it to the string 'London'
Rest API
Hint
Assign the string 'London' to the variable city.
3
Write a function to simulate a REST API call
Define a function called get_weather that takes a parameter city_name and returns the weather from weather_data for that city using weather_data.get(city_name, 'No data')
Rest API
Hint
Use the dictionary's get method to safely get the weather or return 'No data' if the city is not found.
4
Print the weather for the chosen city
Write a print statement that shows the text "Weather in {city}: {weather}" where weather is the result of calling get_weather(city)
Rest API
Hint
Call get_weather with city and use an f-string to print the message.
Practice
(1/5)
1. Why do REST APIs exist in software development?
easy
A. To allow different programs to communicate over the internet using simple rules
B. To create complex user interfaces for websites
C. To store data only on local machines
D. To replace all programming languages with one standard
Solution
Step 1: Understand the purpose of REST APIs
REST APIs are designed to let different software programs talk to each other over the internet easily.
Step 2: Compare options with this purpose
Only To allow different programs to communicate over the internet using simple rules correctly describes this purpose. Options A, B, and C do not relate to REST APIs' main goal.
Final Answer:
To allow different programs to communicate over the internet using simple rules -> Option A
Quick Check:
REST APIs exist for communication = D [OK]
Hint: REST APIs help programs talk over the internet simply [OK]
Common Mistakes:
Thinking REST APIs create user interfaces
Believing REST APIs store data locally only
Assuming REST APIs replace programming languages
2. Which HTTP method is commonly used in REST APIs to retrieve data from a server?
easy
A. GET
B. POST
C. DELETE
D. PUT
Solution
Step 1: Recall HTTP methods in REST APIs
GET is the method used to request and retrieve data from a server.
Step 2: Match the method to the action
POST sends data, DELETE removes data, PUT updates data. Only GET retrieves data.
Final Answer:
GET -> Option A
Quick Check:
Retrieve data = GET [OK]
Hint: GET means get data from server in REST APIs [OK]
Common Mistakes:
Confusing POST with GET for data retrieval
Using DELETE or PUT to get data
Not knowing HTTP method purposes
3. What will be the output of this REST API call?
GET /users/123
Assuming user 123 exists and the API returns JSON data.
medium
A. 500 Internal Server Error
B. {"id":123,"name":"Alice"}
C. 404 Not Found
D. {"error":"User not found"}
Solution
Step 1: Understand the GET request to /users/123
This request asks the server for data about user with ID 123.
Step 2: Check the condition given
The user 123 exists, so the server returns the user's data in JSON format.
Final Answer:
{"id":123,"name":"Alice"} -> Option B
Quick Check:
Existing user GET returns data = C [OK]
Hint: GET existing user returns JSON data, not error [OK]
Common Mistakes:
Choosing error responses when user exists
Confusing 404 with successful data return
Not recognizing JSON format
4. Identify the error in this REST API usage:
POST /users/123
What is wrong if the goal is to update user 123's data?
medium
A. The URL should not include the user ID
B. GET method should be used to update data
C. POST is used instead of PUT for updating existing data
D. DELETE method is required to update data
Solution
Step 1: Understand HTTP methods for REST APIs
POST is usually for creating new data, PUT is for updating existing data.
Step 2: Match method to action
Since the goal is to update user 123, PUT should be used instead of POST.
Final Answer:
POST is used instead of PUT for updating existing data -> Option C
Quick Check:
Update needs PUT, not POST = B [OK]
Hint: Use PUT to update existing data, not POST [OK]
Common Mistakes:
Using POST to update instead of PUT
Thinking GET updates data
Believing DELETE updates data
5. You want to design a REST API that allows clients to list all products and add new products. Which HTTP methods and URLs should you use?
hard
A. DELETE /products to list, PUT /products to add new
B. POST /products to list, GET /products to add new
C. PUT /products to list, DELETE /products to add new
D. GET /products to list, POST /products to add new
Solution
Step 1: Identify methods for listing and adding
GET is used to retrieve or list data, POST is used to create or add new data.
Step 2: Match methods with URLs
Both actions use the /products URL; GET lists products, POST adds new products.
Final Answer:
GET /products to list, POST /products to add new -> Option D
Quick Check:
List = GET, Add = POST = A [OK]
Hint: GET lists, POST adds new resources in REST APIs [OK]
Common Mistakes:
Swapping GET and POST roles
Using DELETE or PUT incorrectly
Confusing URLs for different actions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.